0

In my iPad application, I have an UITextField. User should only type numerical. If user types alphabets or alphanumeric text then there should be an alert message saying "Enter only numerical". What condition should I apply which would find the entered text is numeric, alphabet or alphanumeric? Any help would be highly appreciated.

Thanks and regards

PC

Prateek Chaubey
  • 653
  • 2
  • 10
  • 24
  • Use a regular expression: http://stackoverflow.com/questions/422138/regular-expressions-in-an-objective-c-cocoa-application – isaac May 08 '12 at 19:12

2 Answers2

2

Implement the UITextFieldDelegate method

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string'

You can then reject certain characters inside of that method.

Alternatively, set the keyboard style to: .keyboardType = UIKeyboardTypePhonePad;

Adam Johnson
  • 2,198
  • 1
  • 17
  • 23
  • You beat me by a minute. :) Though, the keyboard style isn't as good an idea as the delegate because of paste...and possibly external keyboards. – Phillip Mills May 08 '12 at 19:18
  • @Adam Johnson: Hi, thanks for your reply. I have changed the keyboard type to numeric. But this keyboard has Alphabetic keyboard option. Can I somehow hide that button? So that user has no option to enter Alphabets.. – Prateek Chaubey May 08 '12 at 20:04
0

Rather than putting up an alert, why not prevent the characters you don't want?

If you implement textField:shouldChangeCharactersInRange:replacementString: you can use it to detect whether the result is acceptable and either allow or disallow the edit.

Phillip Mills
  • 30,888
  • 4
  • 42
  • 57