Reputation: 3010
I'm writing a regular expression to validate a password. The conditions are:
Password must contain at least two special characters
Password must be at least eight characters long
I'm able to make sure that there are atleast 8 characters, atleast one alphabet, atleast one number and atleast one special character using the below Regular expression:
(?=.*[A-z])(?=.*[0-9])(?=.*?[!@#$%\^&*\(\)\-_+=;:'""\/\[\]{},.<>|`]).{8,32}
Only condition i'm not able to get is there must be atleast two special characters (Above Reg exp is atleast one special characters). Does anyone have any idea on this?
Thanks in advance.
Upvotes: 1
Views: 4863
Reputation: 174696
Only condition i'm not able to get is there must be atleast two special characters.
Make it twice by putting the pattern which was present inside the lookahead inside a group and then make it to repeat exactly two times.
^(?=.*[A-Za-z])(?=.*[0-9])(?=(?:.*?[!@#$%\^&*\(\)\-_+=;:'""\/\[\]{},.<>|`]){2}).{8,32}$
If you want to allow atleast 8 characters then you don't need to include 32
inside the range quantifier, just .{8,}
would be enough.
Upvotes: 1