1

I am trying to validate textfield for numbers only.

I am using the following method:

if(isnumber(noOfPassengers.text.intValue))
{
    NSLog(@"value entered correctly.");
    return ;
}
else
NSLog(@"Error: Only numerica values are accepted");

isnumber() function is not working as expected. I would like to validate for numbers only. Got stuck here. Any other inbuilt function to check for numbers or even characters?

user1140780
  • 988
  • 2
  • 13
  • 29
  • 3
    Does this work? If not, what happens that is not as expected? Please be specific in where you are having difficulty. – PengOne Jan 11 '12 at 19:26
  • 1
    possible duplicate of [iPhone how to check that a string is numeric only](http://stackoverflow.com/questions/1320295/iphone-how-to-check-that-a-string-is-numeric-only) – PengOne Jan 11 '12 at 19:33
  • Please could you give more information ie. what is wrong with your method that you used. Does the above method work as planned or is it just buggy code. – Keagan Ladds Jan 11 '12 at 19:28

5 Answers5

4

You haven't posted any code for your function isnumber, so it's impossible to diagnose the problem there. However, writing the function from scratch, here is what I would do:

-(BOOL)isNumeric:(NSString*)inputString{
    NSCharacterSet *alphaNumbersSet = [NSCharacterSet decimalDigitCharacterSet];
    NSCharacterSet *stringSet = [NSCharacterSet characterSetWithCharactersInString:inputString];
    return [alphaNumbersSet isSupersetOfSet:stringSet];
}
PengOne
  • 48,188
  • 17
  • 130
  • 149
3

The correct thing to do here is to implement the UITextFieldDelegate's textField:shouldChangeCharactersInRange:replacementString: to only accept numeric characters.

In the delegate method, return YES and strip any characters that aren't in the decimal character set.

Ben S
  • 68,394
  • 30
  • 171
  • 212
  • The `isnumber()` function takes a C `char` and returns `1` if the char represents a decimal digit, otherwise it returns `0`. It can't do what you're asking for. – Ben S Jan 11 '12 at 19:54
  • I like this better. Better to fix it on the front end instead of letting the user enter something wrong and warning them. Using this alongside a numeric keyboard presentation sounds good to me. – LJ Wilson Jan 11 '12 at 20:37
1

Use NSScanner:

int iValue;

if (noOfPassengers.text.length > 0 && [[NSScanner scannerWithString:noOfPassengers.text] scanInt:&iValue]) {
    //do smomething with iValue (int value from noOfPassengers.text)
    NSLog(@"value entered correctly.");
    return ;
}
else
    NSLog(@"Error: Only numerica values are accepted");
Tomasz Wojtkowiak
  • 4,910
  • 1
  • 28
  • 35
-2

for iOS, you should allow only numeric keyboard to appear instead. user will have no choice to input any other value except numbers..

Saurabh Passolia
  • 8,099
  • 1
  • 26
  • 41
-2

Simple answer to your question is just show Numeric keyboard and check the value then. You don't have to implement UITextFieldDelegate's @Ben.s suggested.

AAV
  • 3,785
  • 8
  • 32
  • 59
  • 1
    Users can still paste non-numeric text or use an external keyboard. If the code assumes a numeric input, this could break the app. – Ben S Jan 11 '12 at 19:55