iphoneiosios5nspredicatenspredicateeditor

NSPredicate: How to insert special caracters in filter (@ $ &)


Fast question, i'm using NSPredicate to verify passwords, I already use this code for the username:

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

But now on the password i would like to allow the user to use special characters such as "@", "$", "&", "*".

How can i do this?


Solution

  • I don't know you needs exactly how many characters or specified characters allow to user. but, according to your question example, if you want to allow the user only @,$,&,* characters. refer a following code.

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

    following filter code is one of the popular password expreesion. alphanumeric characters and select special characters. The password also can not start with a digit, underscore or special character and must contain at least one digit.

    Matches password1 | pa$$word2 | pa!@#$%3

    Non-Matches password | 1stpassword | $password#

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

    and more expression filter about password. see the site: regexlib_password

    following reference is You'll help while you are working. and remember a following Expression about Character and Bracket.

    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 ].