#pragma mark ----- 抽出来 作为公共的方法 小数点 判断 用于 输入 1.2元 逻辑
+ (BOOL)x_JudgmentinputDecimal:(NSString *)textFieldText cont:(NSString *)string
{
NSString *str = [NSString stringWithFormat:@"%@%@", textFieldText, string];
//匹配以0开头的数字
NSPredicate *predicate0 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"^[0][0-9]+$"];
//匹配两位小数、整数
NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"^(([1-9]{1}[0-9]*|[0])\.?[0-9]{0,2})$"];
BOOL sucess = ![predicate0 evaluateWithObject:str] && [predicate1 evaluateWithObject:str] ? YES : NO;
return sucess;
}
在这里用 ,这样用
UITextField 的 UITextFieldDelegate
tf.delegate = self;
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField == self.textF)
{ // 金额输入判断
NSString *str = [NSString stringWithFormat:@"%@%@", textField.text, string];
//匹配以0开头的数字
NSPredicate *predicate0 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"^[0][0-9]+$"];
//匹配两位小数、整数
NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"^(([1-9]{1}[0-9]*|[0])\.?[0-9]{0,2})$"];
BOOL sucess = ![predicate0 evaluateWithObject:str] && [predicate1 evaluateWithObject:str] ? YES : NO;
return sucess;
}
return YES;
}
网友评论