I have a Regex to validate a password: at least one uppercase, one lowercase, one number and one special character:
^(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z])(?=\D*\d)(?=.*[$@$!%*?&,;.:-_])[A-Za-z\d$@$!%*?&,;.:-_]+
But I also want to allow empty because I will validate it in another way.
So I tried to options:
^((?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z])(?=\D*\d)(?=.*[$@$!%*?&,;.:-_])[A-Za-z\d$@$!%*?&,;.:-_]+)*$
NOTE: I wrapped the Regex in () and added *$ at the end;
Or the following:
^$|(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z])(?=\D*\d)(?=.*[$@$!%*?&,;.:-_])[A-Za-z\d$@$!%*?&,;.:-_]+
NOTE: Added $| at start ...
What is the best approach? One of these 2 or some other?
^(?:...)?$^$|^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&,;.:_-])[\w$@$!%*?&,;.:-]+$should work fine.