注:设置(UITextField*)的 delegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSLog(@"%s",__FUNCTION__);
NSString *result = [textField.text stringByReplacingCharactersInRange:range withString:string];
if ([result length] == 0) return YES;
NSString *regex = @"^[a-zA-Z0-9_]{0,20}$";
NSPredicate *prd = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
return [prd evaluateWithObject:result];
}