How to make a text box to accept only 4 digits or digits in iphone

I have an application in which I want to enter a numeric value in a UITextField . But I want to allow only 4 digits to be entered. Thus, 1234 will be valid, but 12345 cannot be entered. Any idea how I can change this to only accept a numerical value limited to 4 digits?

+4
source share
4 answers

Code example:

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSString *currentString = [textField.text stringByReplacingCharactersInRange:range withString:string]; int length = [currentString length]; if (length > 4) { return NO; } return YES; } 
+15
source

The code below helped me in setting the limit that only 2 digits and 4 digits in the text box as input. After entering a two-digit focus, automatically move to the next text box.

Screenshot

 override func viewDidLoad() { super.viewDidLoad() // Set an action on EditingChanged event of textfield txtCardDetails3.addTarget(self, action: #selector(VerifyCardViewController.textFieldDidChange(_:)), forControlEvents: UIControlEvents.EditingChanged) } func textFieldDidChange(textField: UITextField){ let text = textField.text if textField.tag == 101 { // Set tag to textfield (if multiple) during design time if text?.utf16.count==2 { txtCardDetails4.becomeFirstResponder() // Move to next text field } } } func textFieldDidBeginEditing(textField: UITextField) { textField.becomeFirstResponder() } func textFieldShouldReturn(textField: UITextField) -> Bool { textField.resignFirstResponder() return true; } //User can enter two digits in textfield with tag 101 and four digits in textfield with tag 102 func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { if let text = textField.text { let newStr = (text as NSString) .stringByReplacingCharactersInRange(range, withString: string) if newStr.isEmpty { return true } let intvalue = Int(newStr) if textField.tag == 101 { print("101") return (intvalue >= 0 && intvalue <= 99) ? true : false } else if textField.tag == 102 { print("102") return (intvalue >= 0 && intvalue <= 9999) ? true : false } } return true } 
+2
source

Edited by:

First change your type to textFiled keyBoard UIKeyboardTypeNumberPad , so your keyboard only has a numeric digit.

And give the tag UITextField that you want to apply to it (enter only a 4-digit number) , e.g.

 textField.tag = 101; /// this is for, if you have multiple textFiled then it recognize that which textFiled have limit of 4 number ; 

And use the following code method:

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if(textField.tag != 101) return YES; if(string.length == 0) return YES; if(textField.text.length == 4) return NO; NSCharacterSet *nonNumberSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet]; if ([string rangeOfCharacterFromSet:nonNumberSet].location != NSNotFound) { return NO; } return YES; } 
0
source

Swift 3.0 Xcode 8.3.3 ...

First UITextField set the delegate and then put this code ...

 func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { let textstring = (textField.text! as NSString).replacingCharacters(in: range, with: string) let length = textstring.characters.count if length > 4 { return false } return true } 

Objective C Code ...

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSString *textstring = [textField.text stringByReplacingCharactersInRange:range withString:string]; int length = [textstring length]; if (length > 4) { return NO; } return YES; } 
0
source

Source: https://habr.com/ru/post/1499906/


All Articles