Allow only UITextField input numbers

The iPad does not have a Numpad keyboard such as an iPhone / iPod.

I am looking to determine how I can restrict a user keyboard to only accepting values โ€‹โ€‹from 0 to 9.

I would suggest using UITextField "shouldChangeCharactersInRange", but I don't know how best to implement it.

+76
ios cocoa-touch uitextfield uikeyboard ipad
Oct 17
source share
13 answers

Here is how you can solve the problem in the SSN check box, you can change the maximum length and remove the if that checks the keyboard type if you need to.

There is also a logic to suppress warnings about maximum lengths when the user types, rather than inserts data.

In the context of this code, presentAlert()/presentAlert: is just some basic function that represents the UIAlertController (or deprecated UIAlertView ) using the passed message string.

Swift 5

 // NOTE: This code assumes you have set the UITextField(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) 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; } 
+73
Oct 17 '12 at 23:26
source share

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

Before that, set the delegate for textField

  textFieldName.delegate=self; 

or

  [textFieldName setDelegate:self]; 

Than use this code to allow only digit 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; } 
+26
Apr 15 '13 at 12:20
source share

Try this to avoid the problem of clearing the text box.

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 } 
+21
May 25 '17 at 11:01
source share

Very specific steps for Swift code

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

For clarity, these steps assume that your storyboard contains a View Controller with a text box that should accept only numbers.

  • Create a custom class for the view controller that extends the UIViewController . Make sure that the scene in your storyboard belongs to a custom class by setting the value of the custom class in the Xcode Identity Inspector.

     import UIKit class YourCustomController: UIViewController { override func viewDidLoad() { super.viewDidLoad() } } 
  • Create an output from the scene text field in your own view controller.

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

     class YourCustomController: UIViewController, UITextFieldDelegate { ... } 
  • In your custom view manager method, viewDidLoad assign the text field delegate its own view controller class.

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

    As a result of creating a custom view controller, delegate numberField in the previous step, this method will be called every time the user enters a character in the text box. If your method returns true , the character will remain in the text box. If your method returns false , the character will not remain in the text box.

    The string parameter is a character entered by the user. If the string character can be converted to Int , then it is between 0 and 9; otherwise it is an asymmetric 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 controller code).




View controller example with a text box with numbers

 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 } } 



View controller example with decimal text field

If you want to support decimal, use NSNumberFormatter . See Code Comments for 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 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 } } 
+17
Aug 04 '15 at 14:33
source share
 - (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; } 
+8
Aug 03 '13 at 19:07
source share

I applied it 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; } 
+7
Jul 26 '13 at 12:35
source share
 NSString* val = [[textField text] stringByReplacingCharactersInRange:range withString:string]; NSCharacterSet *allowedCharacterSet = [NSCharacterSet decimalDigitCharacterSet]; if ([[string componentsSeparatedByCharactersInSet:[allowedCharacterSet invertedSet]] count] > 1 || [val length] > 5) { return NO; } 
+2
Feb 02 '16 at 17:55
source share
 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; } 
+2
Apr 20 '16 at 6:38
source share

Store various view data from an internal view. There is an easier 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]]]; 
0
Oct. 14 '13 at 12:53 on
source share

I changed @iDev's answer to working on numbers 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; } 
0
Nov 10 '14 at 18:41
source share

If you use my template then the code is as follows

 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 } 
0
Apr 17 '16 at 17:23
source share

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 } 
0
Dec 16 '17 at 8:20
source share

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; } 
-one
Feb 20 '17 at 9:15
source share



All Articles