I need a regular expression for a password field that:
Must have 1 number
Must have 1 letter (uppercase)
Must have 1 letter (low
You can change the base at the end:
((?=.*\d)(?=.*[a-z])(?=.*[A-Z])[^\W_]{8,})
This solution expects your regex engine to be anchored. If not, anchor them with ^$
.
[^\W_]
is negated character class. It asserts that this character is not a word character or _
.
As word characters covers alphanumeric characters and underscores, this double-negated character class shorthand [^\W_]
is well-used for these scenarios.
You can use [[:alnum:]]
as well, if your regex engine supports ascii classes.
Here is a regex demo!
Use [a-zA-Z0-9]
instead of .
and anchor your regex:
^((?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]{8,})$