使用 blocks 为 UITextField 添加实时验证
基于 blocks 的简单方法,并提供完全定制的可能性
NSString *emailPattern = @"^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,4})$";
_emailField.validationBlock = ^(NSString *text) {
return ([text rangeOfString:emailPattern options:NSRegularExpressionSearch].location != NSNotFound );
};
_emailField.postValidationBlock = ^(BOOL valid){
if ( valid ) {
_emailStatus.image = [UIImage imageNamed:@"valid"];
} else {
_emailStatus.image = [UIImage imageNamed:@"invalid"];
}
};
如果您想检查文本框当前是否有效(例如在提交表单之前),只需检查 CCTextField 的 .valid 属性
BOOL fieldIsInValidState = _emailField.valid;
Cocoapods
pod 'CCValidatedTextField', '~> 1.0.1'