iOS TextField输入金额位数限制 正则

2017-05-03  本文已影响751人  Smile_J

判断输入钱的正则表达式,可输入正负,小数点前5位,小数点后2位,位数可控

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;

}

来自 blog.csdn.net/xubaimiao/article/details/41144677 

上一篇下一篇

猜你喜欢

热点阅读