iPhone/iPad : Check for invalid characters in a textbox made for Integers only

Posted by JustinXXVII on Stack Overflow See other posts from Stack Overflow or by JustinXXVII
Published on 2010-04-13T02:48:58Z Indexed on 2010/04/13 2:52 UTC
Read the original article Hit count: 330

Filed under:
|
|
|
|

I noticed that the iPhone OS is pretty good about picking out Integer values when asked to. Specifically, if you use

NSString *stringName = @"6(";
int number = [stringName intValue];

the iPhone OS will pick out the 6 and turn the variable number into 6. However, in more complex mistypes, this also makes the int variable 6:

NSString *stringName = @"6(5";
int number = [stringName intValue];

The iPhone OS misses the other digit, when what could have possibly been the user trying to enter the number 65, the OS only gets the number 6 out of it. I need a solution to check a string for invalid characters and return NO if there is anything other than an unsigned integer in a textbox. This is for iPad, and currently there is no numeric keyboard like the iPhone has, and I'm instead limited to the standard 123 keyboard.

I was thinking that I need to use NSRange and somehow loop through the entire string in the textbox, and checking to see if the current character in the iteration is a number. I'm lost as far as that goes. I can think of testing it against zero, but zero is a valid integer.

Can anyone help?

© Stack Overflow or respective owner

Related posts about iphone

Related posts about ipad