0

Possible Duplicate:
textField:shouldChangeCharactersInRange:replacementString:

I am facing a situation where user is allowed to enter - sign after entering numeric value. How to restrict/avoid this?

Community
  • 1
  • 1
Gowtham
  • 327
  • 8
  • 17

2 Answers2

0

Use NSCharacterSet...

For e.g.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
static NSCharacterSet *charSet3 = nil;
if(!charSet3) {
        charSet3 = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789 "] invertedSet];
    } 
    NSRange location = [string rangeOfCharacterFromSet:charSet3];
    return (location.location == NSNotFound);
}

This will only allow user to enter numeric values to text field and did not take other characters...

Hope this helps..... :)

Aman Aggarwal
  • 3,754
  • 1
  • 19
  • 26
0

If you mean to restrict or check if the entered text is number or not..try this

if ([textField.text isKindOfClass:[NSNumber class]]) {

}
AppleDelegate
  • 4,269
  • 1
  • 20
  • 27