Need Regex Expression for the following password conditions: 1 capital, 1 lower case, 1 special character, 1 numeric, no characters other then alphanumeric and special characters, minimum length of 8, max length of 30
Asked
Active
Viewed 100 times
-3
-
1"no characters other then alphanumeric and special characters ... max length of 30" -- Why? Don't impose arbitrary password restrictions. – Tom Lord May 09 '18 at 09:36
-
2What have you tried so far? What difficulty/errors have you encountered? StackOverflow is a place to ask questions about your code, not a place to get others to write it all for you. – Tom Lord May 09 '18 at 09:38
-
Can you come up with positive and negative examples? And then, at least something that doesn't work just yet. – Tamas Rev May 09 '18 at 10:12
-
You should read [Reference - Password Validation](https://stackoverflow.com/questions/48345922/reference-password-validation) – ctwheels May 30 '18 at 15:38
1 Answers
0
We can do it with lookaheads:
^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[+-_*%#!$/\\"'`])[a-zA-Z0-9+-_*%#!$/\\"'`]{4,30}$
Explanation:
^[a-zA-Z0-9+-_*%#!$/\\"'``]{4,30}$
restricts the length and the type of characters(?=....)
is a lookahead(?=.*[A-Z])
makes sure there is at lest one uppercase character(?=.*[0-9])
makes sure there is at least one number- and so on, the other two lookaheads are for lower case chars and special chars
Demo here.

Tamas Rev
- 7,008
- 5
- 32
- 49