UITextField Should accept number only values UITextField Should accept number only values ios ios

UITextField Should accept number only values


In whatever UITextField you're getting these values from, you can specify the kind of keyboard you want to appear when somebody touches inside the text field.

E.G. a numeric-only keyboard.

Like this screenshot:

numeric only keyboard will appear

This is easily set when working with the XIB and the Interface Builder built into Xcode, but if you want to understand this programmatically, take a look at Apple's UITextInputTraits protocol reference page, specifically the keyboardType property information.

To filter out punctuations, set the textfield's delegate and set up the shouldChangeCharactersInRange method:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{    NSCharacterSet *numbersOnly = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];    NSCharacterSet *characterSetFromTextField = [NSCharacterSet characterSetWithCharactersInString:textField.text];    BOOL stringIsValid = [numbersOnly isSupersetOfSet:characterSetFromTextField];    return stringIsValid;}


Objective C

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{    if (!string.length)         return YES;    if (textField == self.tmpTextField)    {        NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];        NSString *expression = @"^([0-9]+)?(\\.([0-9]{1,2})?)?$";        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression                                                                                options:NSRegularExpressionCaseInsensitive                                                                                  error:nil];        NSUInteger numberOfMatches = [regex numberOfMatchesInString:newString                                                            options:0                                                              range:NSMakeRange(0, [newString length])];                if (numberOfMatches == 0)            return NO;            }    return YES;}

Swift 3.0

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {    if !string.characters.count {        return true    }    do {        if textField == self.tmpTextField {            var newString = textField.text.replacingCharacters(inRange: range, with: string)            var expression = "^([0-9]+)?(\\.([0-9]{1,2})?)?$"            var regex = try NSRegularExpression(pattern: expression, options: NSRegularExpressionCaseInsensitive)            var numberOfMatches = regex.numberOfMatches(inString: newString, options: [], range: NSRange(location: 0, length: newString.characters.count))            if numberOfMatches == 0 {                return false            }        }    }    catch let error {    }    return true}


[textField setKeyboardType:UIKeyboardTypeNumberPad];