UITextField 正则表达式 匹配

注:设置(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];

    

}

你可能感兴趣的:(UITextField 正则表达式 匹配)