I am working on this code and using "match" function to detect strength of password. how can I detect if string has special characters in it?
if(password.match(/[a-z]+/)) score++;
if(password.match(/[A-Z]+/)) score++;
if(password.match(/[0-9]+/)) score++;
If you mean !@#$% and ë as special character you can use:
/[^a-zA-Z ]+/
The ^
means if it is not something like a-z or A-Z or a space.
And if you mean only things like !@$&$ use:
/\W+/
\w
matches word characters, \W
matching not word characters.
You'll have to whitelist them individually, like so:
if(password.match(/[`~!@#\$%\^&\*\(\)\-=_+\\\[\]{}/\?,\.\<\> ...
and so on. Note that you'll have to escape regex control characters with a \
.
While less elegant than /[^A-Za-z0-9]+/
, this will avoid internationalization issues (e.g., will not automatically whitelist Far Eastern Language characters such as Chinese or Japanese).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With