判斷輸入錢的正則表達(dá)式陶因,可輸入正負(fù),小數(shù)點(diǎn)前5位垂蜗,小數(shù)點(diǎn)后2位,位數(shù)可控
UItextField 代理方法
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSString *toString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if (toString.length > 0) {
NSString *stringRegex = @"(\\+|\\-)?(([0]|(0[.]\\d{0,2}))|([1-9]\\d{0,4}(([.]\\d{0,2})?)))?";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", stringRegex];
BOOL flag = [phoneTest evaluateWithObject:toString];
if (!flag) {
return NO;
}
}
return YES;
}