Why does this regular expression fail?

Posted by Stephen on Stack Overflow See other posts from Stack Overflow or by Stephen
Published on 2010-06-10T15:45:04Z Indexed on 2010/06/10 15:52 UTC
Read the original article Hit count: 273

Filed under:
|
|

I have a password validation script in PHP that checks a few different regular expressions, and throws a unique error message depending on which one fails. Here is an array of the regular expressions and the error messages that are thrown if the match fails:

array(
    'rule1' => array(
        '/^.*[\d].*$/i',
        'Password must contain at least one number.'
    ),
    'rule2' => array(
        '/^.*[a-z].*$/i',
        'Password must contain at least one lowercase letter'
    ),
    'rule3' => array(
        '/^.*[A-Z].*$/i',
        'Password must contain at least one uppercase letter'
    ),
    'rule4' => array(
        '/^.*[~!@#$%^&*()_+=].*$/i',
        'Password must contain at least one special character [~!@#$%^&*()_+=]'
    )
);

For some reason, no matter what I pass through the validation, the "Special Characters" rule fails. I'm guessing it's a problem with the expression. If there's a better (or correct) way to write these expressions, I'm all ears!

© Stack Overflow or respective owner

Related posts about php

Related posts about regex