My client has requested that passwords on their system must following a specific set of validation rules, and I'm having great difficulty coming up with a "nice" regular expression.
The rules I have been given are...
- Minimum of 8 character
- Allow any character
- Must have at least one instance from three of the four following character types...
- Upper case character
- Lower case character
- Numeric digit
- "Special Character"
When I pressed more, "Special Characters" are literally everything else (including spaces).
I can easily check for at least one instance for all four, using the following...
^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?\d)(?=.*?[^a-zA-Z0-9]).{8,}$
The following works, but it's horrible and messy...
^((?=.*?[A-Z])(?=.*?[a-z])(?=.*?\d)|(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[^a-zA-Z0-9])|(?=.*?[A-Z])(?=.*?\d)(?=.*?[^a-zA-Z0-9])|(?=.*?[a-z])(?=.*?\d)(?=.*?[^a-zA-Z0-9])).{8,}$
So you don't have to work it out yourself, the above is checking for (1,2,3|1,2,4|1,3,4|2,3,4)
which are the 4 possible combinations of the 4 groups (where the number relates to the "types" in the set of rules).
Is there a "nicer", cleaner or easier way of doing this?
(Please note, this is going to be used in an <asp:RegularExpressionValidator>
control in an ASP.NET website, so therefore needs to be a valid regex for both .NET and javascript.)
<asp:RegularExpressionValidator>
control in several different parts of the website. – Arminius(?=.*[a-z])
is bad in term of performance, multiplicate it by twelve... – Community<asp:CustomValidator>
and write your own function. – Community(?=.*[a-z])
blocks, but unfortunately I'm not in a position to implement anything else. This software is used in multiple locations, and having different custom validation for each just can't happen. That is why I'm stuck with a regex pattern. – Arminius