- (BOOL)validateWtihRegex:(NSString *)regexString {
NSPredicate *regex = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regexString];
return [regex evaluateWithObject:self];
}
#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *currentText = textField.text;
NSString *newText = [currentText stringByReplacingCharactersInRange:range withString:string];
if ([newText isEqualToString:@"."] || [newText isEqualToString:@"00"]) {
return NO;
}
return [newText validateWtihRegex:@"^(0?|[1-9][0-9]*)(\\.[0-9]{0,2})?$"];
}
网友评论