Blueblazer172 Blueblazer172 - 1 year ago 132
PHP Question

password checking function with preg_match

i have this function:

public static function encPasswordCheckFailed($password)
{
if (!preg_match('/[A-Z]+/', $password)){
return true;
}
if (!preg_match('/[a-z]+/', $password)){
return true;
}
if (!preg_match('/[0-9]+/', $password)){
return true;
}
if (!preg_match('/[!@#$%^&*()-+<>]+/', $password)) {
return true;
}

return false;
}


and now i want to force the user to have at least 1 Uppercased Letter, 1 Lowercased Letter, 1 Number and 1 Specialcharacter.


I'm having the problem with this:

if (!preg_match('/[!@#$%^&*()-+<>]+/', $password)) {
return true;
}


All other requirements work. I have only a problem with the specialcharacters.

Can someone tell me the correct pattern for these specialcharacters:
!@#$%^&*()-+<>


i've read some of the answers from here, here and here but they didn't help me further...

Answer Source

The dash/minus character is your issue, as when inside a character group, it denotes a character range.

You need to either:

  • Escape it with a backslash, so that it isn't treated as a special character:
    /[!@#$%^&*()\-+<>]+/
  • Or put it at the end of the pattern, so the PCRE engine knows it can't possibly denote a range:
    /[!@#$%^&*()+<>-]+/

Similarly, the caret (^) is also a special character, denoting negation when inside a character group, but because you didn't put it at the very first position inside the character group, the PCRE engine knows it has no special meaning in your case.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download