I'm trying to create a validation for a password field which allows only the a-zA-Z0-9
characters and .!@#$%^&*()_+-=
I can't seem to get the hang of it.
What's the difference when using regex = /a-zA-Z0-9/g and regex = /[a-zA-Z0-9]/
and which chars from .!@#$%^&*()_+-=
are needed to be escaped?
What I've tried up to now is:
var regex = /a-zA-Z0-9!@#\$%\^\&*\)\(+=._-/g
but with no success
var regex = /^[a-zA-Z0-9!@#\$%\^\&*\)\(+=._-]+$/g
Should work
Also may want to have a minimum length i.e. 6 characters
var regex = /^[a-zA-Z0-9!@#\$%\^\&*\)\(+=._-]{6,}$/g
a sleaker way to match special chars:
/\W|_/g
\W Matches any character that is not a word character (alphanumeric & underscore).
Underscore is considered a special character so add boolean to either match a special character or _
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