用正则表达式使UITextField接受规定的值[转]

只能输入非零开头的正整数用下面的代码

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString
*result = [textField.text stringByReplacingCharactersInRange:range withString:string];
if ([result length] == 0) return YES; // Allow delete all character which are entered.

NSString
*regex = @"^[0-9]*[1-9][0-9]*$";
NSPredicate
*prd = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
return [prd evaluateWithObject:result];
}

你可能感兴趣的:(UITextField)