Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8710199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:33:59+00:00 2026-06-13T04:33:59+00:00

The iPad does not have a Numpad keyboard like the iPhone/iPod does. I’m looking

  • 0

The iPad does not have a “Numpad” keyboard like the iPhone/iPod does.

I’m looking to find how I can restrict the user’s keyboard to only accept values 0 through 9.

I would imagine using UITextField’s “shouldChangeCharactersInRange” but I don’t know the best way to implement it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:34:00+00:00Added an answer on June 13, 2026 at 4:34 am

    This is how you might handle the problem on a SSN verification field, you can modify the max length and remove the if statement checking for keyboard type if you need to.

    There is also logic to suppress the max length alerts when the user is typing as opposed to pasting data.

    Within the context of this code, presentAlert()/presentAlert: is just some basic function that presents a UIAlertController (or a legacy UIAlertView) using the message string passed.

    Swift 5

    // NOTE: This code assumes you have set the UITextField(s)'s delegate property to the 
    // object that will contain this code, because otherwise it would never be called.
    //
    // There are also some better stylistic approaches in Swift to avoid all the 
    // nested statements, but I wanted to keep the styles similar to allow others 
    // to contrast and compare between the two languages a little easier.
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    
        // Handle backspace/delete
        guard !string.isEmpty else {
    
            // Backspace detected, allow text change, no need to process the text any further
            return true
        }
    
        // Input Validation
        // Prevent invalid character input, if keyboard is numberpad
        if textField.keyboardType == .numberPad {
    
            // Check for invalid input characters
            if CharacterSet(charactersIn: "0123456789").isSuperset(of: CharacterSet(charactersIn: string)) {
    
                // Present alert so the user knows what went wrong
                presentAlert("This field accepts only numeric entries.")
    
                // Invalid characters detected, disallow text change
                return false
            }
        }
    
        // Length Processing
        // Need to convert the NSRange to a Swift-appropriate type
        if let text = textField.text, let range = Range(range, in: text) {
    
            let proposedText = text.replacingCharacters(in: range, with: string)
    
            // Check proposed text length does not exceed max character count
            guard proposedText.count <= maxCharacters else {
    
                // Present alert if pasting text
                // easy: pasted data has a length greater than 1; who copy/pastes one character?
                if string.count > 1 {
    
                    // Pasting text, present alert so the user knows what went wrong
                    presentAlert("Paste failed: Maximum character count exceeded.")
                }
    
                // Character count exceeded, disallow text change
                return false
            }
    
            // Only enable the OK/submit button if they have entered all numbers for the last four
            // of their SSN (prevents early submissions/trips to authentication server, etc)
            answerButton.isEnabled = (proposedText.count == 4)
        }
    
        // Allow text change
        return true
    }
    

    Objective-C

    // NOTE: This code assumes you have set the UITextField(s)'s delegate property to the 
    // object that will contain this code, because otherwise it would never be called.
    
    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        // Handle backspace/delete
        if (!string.length)
        {
            // Backspace detected, allow text change, no need to process the text any further
            return YES;
        }
    
        // Input Validation
        // Prevent invalid character input, if keyboard is numberpad
        if (textField.keyboardType == UIKeyboardTypeNumberPad)
        {
            if ([string rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet].invertedSet].location != NSNotFound)
            {
                [self presentAlert: @"This field accepts only numeric entries."];
                return NO;
            }
        }
    
        // Length Validation
        NSString *proposedText = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
        // Check proposed text length does not exceed max character count
        if (proposedText.length > maxCharacters)
        {
            // Present alert if pasting text
            // easy: pasted data has a length greater than 1; who copy/pastes one character?
            if (string.length > 1)
            {
                // Pasting text, present alert so the user knows what went wrong
                [self presentAlert: @"Paste failed: Maximum character count exceeded."];
            }
    
            // Character count exceeded, disallow text change
            return NO;
        }
    
        // Only enable the OK/submit button if they have entered all numbers for the last four
        // of their SSN (prevents early submissions/trips to authentication server, etc)
        self.answerButton.enabled = (proposedText.length == maxCharacters);
    
        // Allow text change
        return YES;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know you can't use GC in iPhone applications cause iPhone does not have
What video format does HTML5 support that is compatible with the iPhone/iPad? I was
I have an iPhone/iPad app which play a HTTP Live Stream of a tv
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); This line of code does not seem to work on the iPad -
I have a UILabel that can be varying lengths depending on whether or not
I have made my application universal for iPhone and iPad (window-based application; universal) from
I just have a quick question. In the 1Password app on the iPhone/iPad when
I have an universal app for both iPhone and iPad. Using below code I
I have a Universal app (iPhone/iPad) which is very basic. There is a List
Does the HTML5 video player on the iPad Safari have the capability to play

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.