21

Possible Duplicate:
Iphone UITextField only integer

I want to place a text field that only accepts numbers (0-9, doesn't even need decimals), but even using the "Number Pad" entry option I still get a keyboard with various symbols on it. Is there a better control for this, is there a better control for what I'm doing, or do I just have to validate input manually?

Community
  • 1
  • 1
RCIX
  • 38,647
  • 50
  • 150
  • 207
  • I don't think this is a duplicate. The linked question asks how to detect whether non-numeric characters were entered in a test field; this question asks how to *prevent* non-numeric characters from being entered. – Jon Schneider Oct 18 '18 at 12:39

3 Answers3

31
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
/* for backspace */    
    if([string length]==0){
       return YES;
    }

/*  limit to only numeric characters  */

    NSCharacterSet *myCharSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    for (int i = 0; i < [string length]; i++) {
       unichar c = [string characterAtIndex:i];
       if ([myCharSet characterIsMember:c]) {
          return YES;
      }
    }

return NO;
}
Rakesh Bhatt
  • 4,606
  • 3
  • 25
  • 38
24

The code is somehow incorrect, should be

/*  limit to only numeric characters  */
NSCharacterSet* numberCharSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
for (int i = 0; i < [string length]; ++i)
{
    unichar c = [string characterAtIndex:i];
    if (![numberCharSet characterIsMember:c])
    {
        return NO;
    }
}

return YES;
Zheng Te
  • 427
  • 3
  • 9
  • This one looks to check all chars in the set for me. check it... this could be the accepted answer. Make sure you place this inside the shouldChangeCharactersInRange delegate called method. Apple documentation recommends you check for numbers within this delegate method. (From shouldChangeCharactersInRange Apple doc ) Discussion The text field calls this method whenever user actions cause its text to change. Use this method to validate text as it is typed by the user. For example, you could use this method to prevent the user from entering anything but numerical values. – Matthew Ferguson Sep 05 '18 at 21:37
20

The following code will allow you to only input numbers as well as limit the amount of characters that can be used.

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{
    /*  limit to only numeric characters  */
    NSCharacterSet *myCharSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    for (int i = 0; i < [string length]; i++) {
        unichar c = [string characterAtIndex:i];
        if ([myCharSet characterIsMember:c]) {
            return YES;
        }
    }

    /*  limit the users input to only 9 characters  */
    NSUInteger newLength = [customTextField.text length] + [string length] - range.length;
    return (newLength > 9) ? NO : YES;
}
WrightsCS
  • 50,551
  • 22
  • 134
  • 186
  • 5
    I think using this code you can't use BackSpace for Editing. – Ankit Vyas Dec 30 '11 at 05:32
  • Yes from above code you can't use backspace and I have tried this on iPad application – user366584 Dec 30 '12 at 05:40
  • This may be a duplicate but the answer worked better for me. – hippeelee Feb 15 '13 at 01:39
  • 3
    This code is logically wrong. You don't want to break out of the loop by returning YES after validating the 1st character. See Zheng Te's answer. – foson Apr 02 '13 at 12:55
  • 4
    This code is wrong, you are leaving the loop after the first valid character. A string "1dasdas" is valid, accordingly to this. – Crystian Leão May 29 '13 at 05:03
  • What if if you have localisation in your project. It wont work. – Shreesh Garg Jan 22 '14 at 07:10
  • This code breaks out after 1st loop on your return. This is a common error, you should instead have a BOOL called stringIsValid=YES, and then set it to NO when ![myCharSet characterIsMember:c] and then break out and return the value of stringIsValid – Pavel Gurov Sep 17 '14 at 13:02
  • @Zheng Te's answer below works correctly for me. – kmak Apr 12 '16 at 20:24