Validating Complexity and Length of Passwords

Q

How to write a regular expression to check the complexity and length of passwords with the following requirements:

  • Password must be at least 6 characters long
  • Password must include at least including 1 upper case letter
  • Password must include at least including 1 lower case letter
  • Password must include at least including 1 special character
  • Password must include at least including 1 numeric digit

For example, Br1ckBe@k is a valid password.

✍: Guest

A

Here is the regular expression to validate passwords that meet your requirements:

(?=.*[A-Z])(?=.*[a-z])(?=.*\W)(?=.*\d).{8,}

(?=.*[A-Z])   - Look-ahead for a upper case letter
(?=.*[a-z])   - Look-ahead for a lower case letter
(?=.*\W)      - Look-ahead for a special character
(?=.*\d)      - Look-ahead for a numeric digit
.{8,}         - Matches 8 or more characters

Click the button to test this regular expression here online:

2013-01-29, 2365👍, 0💬