I need to allow the end user enter only 1-9 digits:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if([newString length]>=1)
{
NSString *sepStr=[newString substringToIndex:1];
return !([sepStr length]>1);
}
if ([string length] == 0 && range.length > 0){
textField.text = [textField.text stringByReplacingCharactersInRange:range withString:string];
return NO;
}
NSCharacterSet *nonNumberSet = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet];
if ([string stringByTrimmingCharactersInSet:nonNumberSet].length > 0)return YES;
return NO;
}
But it’s still possible to enter more than 1 number.
Please help to find an error.
Just add this to your first test :