Allow only Numbers for UITextField input
Asked Answered
B

16

86

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.

Brunson answered 17/10, 2012 at 23:9 Comment(2)
I made a tutorial on how to accomplish this with downloadable project source code. Here: xcodenoobies.blogspot.com/2013/12/…Erective
Go through the link #10735459Propitious
S
88

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;
}
Sniggle answered 17/10, 2012 at 23:26 Comment(5)
Thanks! The 'remove invalid characters from input, if keyboard is numberpad' section helped answer my question!Brunson
@Gargo the question specifically states that the only values allowed should be the digits 0 through 9. The period character does not fall within those requirements. For allowing the period character, one can see the answer given by Aje here.Sniggle
already used it but it leaves the problem with leading zeroesBundelkhand
@Bundelkhand you can use something similar to what he has for detecting other period characters and only return yes for a zero character if: the text field is currently empty, if the insertion point is at index 0 and the next character is a period, or if the insertion point is at an index greater than that of an existing period character. At least that would be one way I might check to make sure the zero being entered will not create a leading zero problem.Sniggle
I think CharacterSet(charactersIn: "0123456789").isSuperset(of: CharacterSet(charactersIn: string)) should be !CharacterSet(charactersIn: "0123456789").isSuperset(of: CharacterSet(charactersIn: string)).Ulrich
B
29

You can use this code to allow only number in textField.

Before that set delegate for textField

      textFieldName.delegate=self;

or

      [textFieldName setDelegate:self];

Than use this code to allow only digit to textField

      - (BOOL) textField: (UITextField *)theTextField shouldChangeCharactersInRange:(NSRange)range replacementString: (NSString *)string {
//return yes or no after comparing the characters

      // allow backspace
      if (!string.length)
      {
           return YES;
      }

      ////for Decimal value start//////This code use use for allowing single decimal value
      //    if ([theTextField.text rangeOfString:@"."].location == NSNotFound)
      //    {
      //        if ([string isEqualToString:@"."]) {
      //            return YES;
      //        }
      //    }
      //    else
      //    {
      //        if ([[theTextField.text substringFromIndex:[theTextField.text rangeOfString:@"."].location] length]>2)   // this allow 2 digit after decimal 
      //        {
      //            return NO;
      //        }
      //    }
      ////for Decimal value End//////This code use use for allowing single decimal value

      // allow digit 0 to 9
      if ([string intValue])
      {
            return YES;
      }

      return NO;
    }
Boru answered 15/4, 2013 at 12:20 Comment(3)
btw, for others using this code, [string intValue] returns 0 for @"0" - so if ([string intValue]) is not met for @"0". Better to use if ([string rangeOfCharacterFromSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]].location != NSNotFound)Unfailing
@".".intValue is 0. And @"0".intValue is 0 too.Wraf
To clarify the other comments here: This code does not let the user type a zero (0) character.Sniggle
O
25

Try this to avoid textfield clearing issue

Swift 3.0

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    guard NSCharacterSet(charactersInString: "0123456789").isSupersetOfSet(NSCharacterSet(charactersInString: string)) else {
        return false
    }
    return true
}

Swift 4.0

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    guard CharacterSet(charactersIn: "0123456789").isSuperset(of: CharacterSet(charactersIn: string)) else {
        return false
    }
    return true
}
Oriya answered 25/5, 2017 at 11:1 Comment(3)
You can simplify delegate method and just left return guard CharacterSet(charactersIn: "0123456789").isSuperset(of: CharacterSet(charactersIn: string))Catgut
I copied and pasted onto my code and doesn't work. How do I connect it and make it work?Apache
first set textField delegate (textField.delegate = self) and conform UITextFieldDelegate protocol that's it.Oriya
B
19

Very specific steps for Swift code

You can provide logic that restricts the text field's input in the func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool method by implementing the UITextFieldDelegate protocol.

For the sake of clarity, these steps assume that your storyboard contains a View Controller with a text field object that should only accept digits.

  1. Create a custom class for the view controller that extends UIViewController. Make sure that the scene in your storyboard refers to the custom class by setting the custom class value in Xcode's Identity Inspector.

    import UIKit
    class YourCustomController: UIViewController {
        override func viewDidLoad() {        
            super.viewDidLoad()
        }
    }
    
  2. Create an outlet from your scene's text field to your custom View Controller.

    class YourCustomController: UIViewController {
        @IBOutlet weak var numberField: UITextField!
        ...
    }
    
  3. Apply the UITextFieldDelegate protocol in your custom view controller.

    class YourCustomController: UIViewController, UITextFieldDelegate {
        ...
    }
    
  4. In your custom view controller's viewDidLoad method, assign your text field's delegate to your custom view controller class.

    override func viewDidLoad() {        
        super.viewDidLoad()
        numberField.delegate = self
    }
    
  5. Add the UITextFieldDelegate's func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool method.

    As a result of making your custom view controller the numberField's delegate in the previous step, this method will be called each time a user enters a character into the text field. If your method returns true then the character will remain in the text field. If your method returns false then the character will not remain in the text field.

    The string parameter is the character being entered by the user. If the string character can be converted to an Int then it is between 0 and 9; otherwise, it is some non-number character.

    class YourCustomController: UIViewController, UITextFieldDelegate {
        ...
        func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    
            return Int(string) != nil
        }
    }
    

(See below for the full view controller code.)


Example View Controller with digits only text field

import UIKit

class YourCustomController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var numberField: UITextField!

    override func viewDidLoad() {        
        super.viewDidLoad()       
        numberField.delegate = self
    }

    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {        
        return Int(string) != nil
    }    
}

Example View Controller with a Decimal text field

If you want to support a decimal number then take advantage of NSNumberFormatter. See the code comments for the differences.

import UIKit

class YourCustomController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var numberField: UITextField!

    private var formatter: NSNumberFormatter!

    override func viewDidLoad() {        
        super.viewDidLoad()       
        numberField.delegate = self

        // Initialize the formatter; minimum value is set to zero; style is Decimal. 
        formatter = NSNumberFormatter()
        formatter.numberStyle = NSNumberFormatterStyle.DecimalStyle
        formatter.minimum = 0
    }

    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        // Combine the current text field value and the new string
        // character. If it conforms to the formatter's settings then
        // it is valid. If it doesn't then nil is returned and the
        // string character should not be allowed in the text field.         
        return formatter.numberFromString("\(textField.text)\(string)") != nil
    }    
}
Brahma answered 4/8, 2015 at 14:33 Comment(8)
This is good but made a small adjustment as it doesn't allow you to delete anything in the field without a check for empty string. I also added ability for negative by checking the first character if (string=="-" && range.location==0) || string=="" { return true } return string.toInt() != nilLungwort
return string.toInt() != nil Worked like a charm. Thanks!Metabolism
Note in Swift 2 I had to change this to return Int(string) != nilHerbivore
@Herbivore Thanks for the comment! I think most Swift developers have moved or will be moving to Swift 2 and above. Therefore, I updated the answer to conform to Swift 2's string-to-int conversion.Brahma
One further change I had to make - the delete key no longer worked! So I changed it to return string == "" || Int(string) != nilHerbivore
Is there any way which will accept a single '.' to represent a decimal point?Levulose
The above code under Example View Controller with a Decimal text field will prevent a user to enter more than one decimal. It makes use of the NSNumberFormatter which will disregard an entry input that doesn't convert into a decimal number.Brahma
Won't textField.text return Optional("<someText>")?Prairial
T
9
- (BOOL) textField: (UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString: (NSString *)string {

    NSNumberFormatter * nf = [[NSNumberFormatter alloc] init];
    [nf setNumberStyle:NSNumberFormatterNoStyle];

    NSString * newString = [NSString stringWithFormat:@"%@%@",textField.text,string];
    NSNumber * number = [nf numberFromString:newString];

    if (number)
        return YES;
    else
       return NO;
}
Touber answered 3/8, 2013 at 19:7 Comment(1)
This works fine for fraction juste you need change the right newString: NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];Sematic
P
8

swift 5

    //MARK:- UITextFieldDelegate

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    
    let allowedCharacters = "1234567890"
    let allowedCharacterSet = CharacterSet(charactersIn: allowedCharacters)
    let typedCharacterSet = CharacterSet(charactersIn: string)
    return allowedCharacterSet.isSuperset(of: typedCharacterSet)
}

You can now just tap 1234567890 only

Penley answered 28/4, 2020 at 1:5 Comment(2)
how do you implement this? just creating this function won't connect it to UITextfieldApache
you have to add: textField.delegate = self in viewDidLoad()Sickly
M
7

I applied this and it works!!

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    // Check for non-numeric characters
    NSUInteger lengthOfString = string.length;
    for (NSInteger index = 0; index < lengthOfString; index++) {
        unichar character = [string characterAtIndex:index];
        if (character < 48) return NO; // 48 unichar for 0
        if (character > 57) return NO; // 57 unichar for 9
    }
    // Check total length for restrict user
    NSUInteger proposedNewLength = textField.text.length - range.length + string.length;
    if (proposedNewLength > 6)
        return YES;
    return YES;                                                                                                                                     
}
Manado answered 26/7, 2013 at 12:35 Comment(1)
To add ".", replace if (character < 48) return NO; // 48 unichar for 0 if (character > 57) return NO; // 57 unichar for 9 with if ((character < 48 || character > 57) && character != 46) I would additionally recommend you compare character to a hexadecimal representations of the numbers as hexadecimals are most commonly used in these circumstances. I.e. if ((character < 0x30 || character > 0x39) && character != 0x2E)Vouvray
M
2
NSString* val = [[textField text] stringByReplacingCharactersInRange:range withString:string];
    NSCharacterSet *allowedCharacterSet = [NSCharacterSet decimalDigitCharacterSet];
    if ([[string componentsSeparatedByCharactersInSet:[allowedCharacterSet invertedSet]] count] > 1 || [val length] > 5) {
        return NO;
    }
Mattoid answered 2/2, 2016 at 17:55 Comment(0)
A
1
Works fine for me :

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (([string rangeOfCharacterFromSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]].location != NSNotFound) && !(range.length==1 && string.length==0)) {
            return NO;
        }
        return YES;
    }
Awlwort answered 20/4, 2016 at 6:38 Comment(0)
C
1

In Swift:

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        return string.isEmpty || Int(string) != nil
    }
Chiro answered 25/3, 2020 at 3:21 Comment(0)
B
1

in swift 5.9:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    
        textfield.delegate = self
    }

    @IBOutlet weak var textfield: UITextField!    
}
extension ViewController: UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let filtered = textField.text!.filter{"0123456789".contains($0)}
        if filtered != textField.text {
            textField.text = filtered
        }
        return true
    }
}
Battement answered 10/10, 2023 at 8:30 Comment(0)
R
0

Keep distinct presentation data from internal representation. There is a simpler way. Let NSNumberFormatter do the job:

 NSNumberFormatter* ns = [[NSNumberFormatter alloc] init];
 ns.numberStyle = NSNumberFormatterDecimalStyle;
 [ns setMaximumFractionDigits:2];
 // This is your internal representation of the localized number
 double a = [[ns numberFromString:self.textIVA.text] doubleValue]];

[mylabel setText:[NSString stringWithFormat:@"€ %@",
     [NSNumberFormatter localizedStringFromNumber:
                          [NSNumber numberWithDouble:a]
                                      numberStyle:NSNumberFormatterDecimalStyle]]];
Ribosome answered 14/10, 2013 at 12:53 Comment(0)
D
0

I have modified @iDev's answer to work for digitals and ".":

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
     // Check for non-numeric characters
     NSUInteger lengthOfString = string.length;
     for (NSInteger index = 0; index < lengthOfString; index++) {
         unichar character = [string characterAtIndex:index];
         if ((character < 48) && (character != 46)) return NO; 
         // 48 unichar for 0, and 46 unichar for point
         if (character > 57) return NO; 
         // 57 unichar for 9
     }
     // Check for total length
     NSUInteger proposedNewLength = textField.text.length - range.length + string.length;
     if (proposedNewLength > 6)
         return YES;
     return YES; 
 }
Disaffirm answered 10/11, 2014 at 18:41 Comment(0)
R
0

If you use my specification pattern then code looks like this

textField.delegate = self

lazy var specification: Specification = {
    return RegularExpressionSpecification(pattern: "^(|0|[1-9]\\d{0,6})$")
}()

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    let textFieldString: NSString = textField.text ?? ""
    let s = textFieldString.stringByReplacingCharactersInRange(range, withString:string)
    return specification.isSatisfiedBy(s)
}

func textFieldShouldReturn(textField: UITextField) -> Bool {
    let s = textField.text ?? ""
    let isTextValid = specification.isSatisfiedBy(s)
    if isTextValid {
        textField.resignFirstResponder()
    }
    return false
}
Rozellarozelle answered 17/4, 2016 at 17:23 Comment(2)
How would I restrict the UITextfield to only receive numbers and limit the amount of numbers between 6 and 8?Danette
Hi @MarcoAlmeida take a look at my framework SwiftyFORM, it can live validate text, github.com/neoneye/SwiftyFORMRozellarozelle
L
0

swift 3

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if textField==yourTextFieldOutlet {
                if(CharacterSet.decimalDigits.isSuperset(of: CharacterSet(charactersIn: yourTextFieldOutlet.text!))){
//if numbers only, then your code here
                }
                else{
                showAlert(title: "Error",message: "Enter Number only",type: "failure")
                }
            }
    return true
    }
Lorelle answered 16/12, 2017 at 8:20 Comment(0)
D
-1

Use this code:

NSString* val = [[textField text] stringByReplacingCharactersInRange:range withString:string];
NSCharacterSet *allowedCharacterSet = [NSCharacterSet decimalDigitCharacterSet];
if ([[string componentsSeparatedByCharactersInSet:[allowedCharacterSet invertedSet]] count] > 1 || [val length] > 5) {
    return NO;
}
Denning answered 20/2, 2017 at 9:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.