Regular expression (regex) for password checking
What is the correct regex
password to meet the following criteria:
- Must include at least 1 lowercase letter.
- Must include at least 1 uppercase letter.
- Must include at least 1 number.
- Must include at least 1 special character (only the following special characters are allowed:)
!#%
. - You should NOT include any other characters, followed by
A-Za-z0-9!#%
(optional;
). - Must be 8 to 32 characters long.
This is what I tried, but it doesn't work:
^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[\!\#\@\$\%\&\/\(\)\=\?\*\-\+\-\_\.\:\;\,\]\[\{\}\^]).{8,32}
But it should be:
^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[\!\#\@\$\%\&\/\(\)\=\?\*\-\+\-\_\.\:\;\,\]\[\{\}\^])[A-Za-z0-9!#%]{8,32}
But Unihedron's solution is better anyway, just wanted to mention it for users who will read this question in the future. :)
Unihedron's solution (can also be found in his answer below, I copied it for myself, just in case it changes (updates it to a better version) in response):
^(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z])(?=\D*\d)(?=.*?[!#%])[A-Za-z0-9!#%]{8,32}$
I got the following regex
:
^(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z])(?=\D*\d)(?=.*?[\!\#\@\$\%\&\/\(\)\=\?\*\-\+\-\_\.\:\;\,\]\[\{\}\^])[A-Za-z0-9\!\#\@\$\%\&\/\(\)\=\?\*\-\+\-\_\.\:\;\,\]\[\{\}\^]{8,60}$
Thanks again to Unihedron and scamazin. Appreciated!
+1
source to share
2 answers
Use this regex:
/^(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z])(?=\D*\d)(?=[^!#%]*[!#%])[A-Za-z0-9!#%]{8,32}$/
Here is the regex demo !
More details:
+5
source to share