-2

Possible Duplicate:
Use regular expressions to validate passwords

I'm trying to code a php part where I have to make sure password is:

  1. At least 6 characters long
  2. At least one uppercase letter
  3. At least one lowercase letter
  4. At Least one numeric character
  5. At least one non-alphanumeric character

I have to make sure that the password matches the "Re-Enter password" section. I have the validation for password and password retype, is this the best way to express that?

if ($pass != $repass)
{
  $repassErrorMsg = "Your password does not match the password entered above";
}

I am not sure how I can validate password to the cases listed above. I know how to do RegEx to verify solid formats, but I am not sure how to validate the following case if format is flexible. How can I express my password RegEx to validate any of the case scenarios (ex. at least one numeric) for the password the the user chooses. He may have any of the case scenarios entered anywhere in the password textbox.

Community
  • 1
  • 1
GivenPie
  • 1,451
  • 9
  • 36
  • 57
  • 1
    What have you tried? Also, be aware of the fact that PHP is loosely typed, you should atleast be doing `$pass !== $repass` instead. – Zar Sep 26 '12 at 21:09
  • @Zar Okay, the functions of !== and != are the same? – GivenPie Sep 26 '12 at 21:13
  • @David B thanks I will look into that page as well. – GivenPie Sep 26 '12 at 21:14
  • @GivenPie The opposite. `1 == "1doesntmatter"` would equal true; not what you'd expect right? (Proof: http://codepad.org/NNiaZMM0) – Zar Sep 26 '12 at 21:49

4 Answers4

1

To check the strength and safety of a password, you have to use a mix of Regex and PHP.

Check this post. The code provided describes all your conditions.

Ωmega
  • 42,614
  • 34
  • 134
  • 203
0

I use PHP-Password class - it does everything you have asked for.

Ωmega
  • 42,614
  • 34
  • 134
  • 203
0

I found this that might help...

Test your upper and lower case:

checking a password for upper lower numbers and symbols

Test your length:

Check String Length In PHP

Community
  • 1
  • 1
Stephen
  • 576
  • 8
  • 19
0

Using regex it is very simple >>

preg_match('/^(?=.{6})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[\W_])/', $password))

Test this code here.

Ωmega
  • 42,614
  • 34
  • 134
  • 203