5
I am writing a regular expression for password validation.
I wanted to know the easiest way to get a regular expression to accept, at the very least, two of these conditions:
- Capital letters
- Lowercase letters
- Special characters
- Numbers
Right now, I have the following regular expression:
"(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{4,20}";
in which:
(?=.*\\d): Accept at least one number.
(?=.*[a-z]): Accept at least one lower case letter.
(?=.*[A-Z]): Accept at least one capital letter.
(?=.*[@#$%]): Accept at least one symbol.
{4,20}: Password between 4 and 20 characters.
The problem is that in order for the password to pass this validation process, the password will have to have obligatorily one of these symbols. That is, a password must have a number, a lowercase letter, a capital letter and a symbol.
What I want is for the password to respect two or more conditions.
Example:
- A password with a lowercase letter and a symbol is accepted (goes from meeting the two conditions).
- A password with a lowercase letter, a capital letter and a symbol is also accepted.
- A lowercase only password is not accepted.
Test with
______.– Guilherme Lautert
Thank you, @Guilhermelautert . I’ve updated my reply to also cover these other special characters.
– Pedro Corso
Tip: when you are working with anchors not the need for poles between brackets,
[\d]+suffice\d+. Second you could have used only(\W|_)+. And out of curiosity there is also the class Posix[:alnum:], getting[^[:alnum:]]+– Guilherme Lautert