I'm trying to come up with a regex that validates passwords. The restrictions are as follows:
Must be at least two of the following:
- one lowercase [a-z],
- one uppercase [A-Z],
- one digit [\d],
- one special character [!@#\$%\^\&*)(+=._-].
must not begin or end with white-space but can contain white-spaces inside,
- must be between 7 and 20 characters long.
So far, this is the last version of what I've come up with:
^(?=.{7,20}$)(?:(?=.*[\d!@#\$%\^\&*\)\(+=._-])(?=.*[a-z])\S*|(?=.*[A-Z])(?=.*[\d!@#\$%\^\&*\)\(+=._-])\S*|(?=.*[A-Z])(?=.*[a-z])\S*|(?=.*[\d)\(+=._-])(?=.*[!@#\$%\^\&*\)\(+=._-])\S*)$
This works for all of the above except letting white-spaces inside. I've gone through multiple regex and this is the best one so far (but also the ugliest).
Edit: Thank you for the fast replies. Why these requirements are in place is beside the point. I know passwords would be more secure if all of the above were required. But as not all customers use password managers... Now, why is this not a duplicate question? Because no other thread requires any two of the above. They simply start with requiring specific two, than adding another one and so on. This needs to be any two conditions.