0

快速的问题,我NSPredicate用来验证密码,我已经将此代码用于用户名:

-(BOOL)isUserValid:(NSString *)checkString{
    NSString       *filter = @"[A-Z0-9a-z]{5,40}";
    NSPredicate *evaluator = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", filter];
    return [evaluator evaluateWithObject:checkString];
}

但现在在密码上,我想允许用户使用特殊字符,例如“@”、“$”、“&”、“*”。

我怎样才能做到这一点?

4

1 回答 1

4

我不知道您究竟需要多少个字符或指定的字符允许用户使用。但是,根据您的问题示例,如果您只想允许用户使用@, $, &,*字符。参考以下代码。

-(BOOL)isUserValid:(NSString *)checkString{
    NSString       *filter = @"[A-Z0-9a-z@$&*]{5,40}";
    NSPredicate *evaluator = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", filter];
    return [evaluator evaluateWithObject:checkString];
}

以下过滤器代码是流行的密码表达式之一。字母数字字符并选择特殊字符。密码也不能以数字、下划线或特殊字符开头,并且必须至少包含一位数字。

匹配密码1 | pa$$word2 | 爸!@#$%3

密码不匹配 | 第一个密码 | $密码#

-(BOOL)isUserValid:(NSString *)checkString{
        NSString       *filter = @"^(?=[^\\d_].*?\\d)\\w(\\w|[!@#$%]){5,40}";
        NSPredicate *evaluator = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", filter];
        return [evaluator evaluateWithObject:checkString];
    }

以及更多关于密码的表达式过滤器。查看网站:regexlib_password

以下参考是您在工作时会提供帮助。并记住以下关于字符和括号的表达式。

Character Classes

.   Matches any character except newline. Will also match newline if single-line mode is enabled.
\s  Matches white space characters.
\S  Matches anything but white space characters.
\d  Matches digits. Equivalent to [0-9].
\D  Matches anything but digits. Equivalent to [^0-9].
\w  Matches letters, digits and underscores. Equivalent to [A-Za-z0-9_].
\W  Matches anything but letters, digits and underscores. Equivalent to [^A-Za-z0-9_].
\xff    Matches ASCII hexadecimal character ff.
\x{ffff}    Matches UTF-8 hexadecimal character ffff.
\cA Matches ASCII control character ^A. Control characters are case insensitive.
\132    Matches ASCII octal character 132.

Bracket Expressions

[adf?%] Matches characters a or d or f or ? or %.
[^adf]  Matches anything but characters a, d and f.
[a-f]   Match any lowercase letter between a and f inclusive.
[A-F]   Match any uppercase letter between A and F inclusive.
[0-9]   Match any digit between 0 and 9 inclusive. Does not support using numbers larger than 9, such as [10-20].

[:upper:]   Matches uppercase letters. Equivalent to A-Z.
[:lower:]   Matches lowercase letters. Equivalent to a-z.
[:alpha:]   Matches letters. Equivalent to A-Za-z.
[:alnum:]   Matches letters and digits. Equivalent to A-Za-z0-9.
[:ascii:]   Matches ASCII characters. Equivalent to \x00-\x7f.
[:word:]    Matches letters, digits and underscores. Equivalent to \w.
[:digit:]   Matches digits. Equivalent to 0-9.
[:xdigit:]  Matches characters that can be used in hexadecimal codes. Equivalent to A-Fa-f0-9.
[:punct:]   Matches punctuation.
[:blank:]   Matches space and tab. Equivalent to [ \t].
[:space:]   Matches space, tab and newline. Equivalent to \s.
[:cntrl:]   Matches control characters. Equivalent to [\x00-\x1F\x7F].
[:graph:]   Matches printed characters. Equivalent to [\x21-\x7E].
[:print:]   Matches printed characters and spaces. Equivalent to [\x21-\x7E ].
于 2012-08-20T18:03:11.107 回答