Regular expression for password (at least 2 digits and one special character and minimum length 8)

Something like this should do the trick.

^(?=(.*\d){2})(?=.*[a-zA-Z])(?=.*[!@#$%])[0-9a-zA-Z!@#$%]{8,}

(?=(.*\d){2}) - uses lookahead (?=) and says the password must contain at least 2 digits

(?=.*[a-zA-Z]) - uses lookahead and says the password must contain an alpha

(?=.*[!@#$%]) - uses lookahead and says the password must contain 1 or more special characters which are defined

[0-9a-zA-Z!@#$%] - dictates the allowed characters

{8,} - says the password must be at least 8 characters long

It might need a little tweaking e.g. specifying exactly which special characters you need but it should do the trick.

Leave a Comment