The reason why the string is not accepted is because you forgot the slash with d
, and the regex requires the letter d
to be inside the string. Fix is
^(?=.{8,})(?=.*\d)(?=.*[A-Z])(?=.*[!@#$%^&*? ]).*$
^ ^^
Use it with a i
modifier. See demo
[A-z]
issue is a well-known one. Also, the initial .*
should be removed, or some parts of the regex won't validate correctly.
And speaking about optimizations: length checking can almost always be moved to the end:
^(?=.*\d)(?=.*[A-Z])(?=.*[!@#$%^&*? ]).{8,}$
See another demo (again, note i
modifier).
Also, see Fine-Tuning: Removing One Condition:
If you must check for n
conditions, your pattern only needs to include n-1
lookaheads at the most. Often, you are even able to combine several conditions into a single lookahead.
And as far as your conditions are as above (1) minimum 8 characters, 2) minimum 1 special character, 3) minimum 1 number) - there is no English letter requirement - you can even use
^(?=.*\d)(?=.*[!@#$%^&*? ]).{8,}$