Create preg_match for password validation allowing (!@#$%)

Viewed 79638

I would like to create a preg_match function to validate my passowrds, but I'm not sure how to write it to allow the following special characters to be used: !@#$%.

if(!preg_match(?????)$/', $password))

Here are my password rules that I want to work into the regex:

  • May contain letter and numbers
  • Must contain at least 1 number and 1 letter
  • May contain any of these characters: !@#$%
  • Must be 8-12 characters

Thank you for any help you can offer.

9 Answers

A different way

Let look different to this, I believe that users can use all of the ASCII valid characters (32 < ascii_code < 127) in the password and created a function that checks all the characters that are is in ASCII table!

function check($pass, $i = 0){
     if(strlen($pass) > $i)
         return (0rd(pass[$i]) > 32 and 0rd(pass[$i]) < 127) and check($pass, $i + 1);
}

Usage

if(!check($_POST["password"])){
    echo "password is wrong";
}
Related