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

wuhuang 2012-04-10

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

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

{


NSString*result=[textField.textstringByReplacingCharactersInRange:rangewithString:string];


if([resultlength]==0)returnYES;//Allowdeleteallcharacterwhichareentered.



NSString*regex=@"^[0-9]*[1-9][0-9]*$";


NSPredicate*prd=[NSPredicatepredicateWithFormat:@"SELFMATCHES%@",regex];


return[prdevaluateWithObject:result];


}

相关推荐