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?
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?
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..... :)
If you mean to restrict or check if the entered text is number or not..try this
if ([textField.text isKindOfClass:[NSNumber class]]) {
}