Skip to content

Instantly share code, notes, and snippets.

@stuart-rickard
stuart-rickard / regex-tutorial.md
Last active March 6, 2022 19:43
Password Validity Regex Tutorial

Password Validity Regex Tutorial

Let's say the security policy for your website requires users to provide a password. Let's say the password must be at least 6 but not more than 12 characters long. Let's say the allowed characters in a password are lowercase and uppercase letters, numbers, and these special characters: !, @, #, $, and %. Is there a simple way to check whether a user-entered string complies with this policy? Yes - there is; read on to find out how!

Summary

We can write a regular expression or "regex" to check whether a string entered by a user complies with this policy. Here is this regex:

/^[a-zA-Z0-9!@#$%]{6,12}$/