I wanted to allow regex to accept minimum of 8 characters and any 3 combinations out of following 4 categories.
- One uppercase alpha character
- One lowercase alpha character
- One numeric character
- One special character
The good thing is, there are many regex available for my requirement but most of them allows space.
The below regex meets my requirement but it allows space as well.
^.*(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[\d\W])(?=.*[!@#\$%&/=?_\.,:;-\\]).*$
and this one also allows space
^.*(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[\d\W])(?=.*[\S])
String should match:
Testing123$
Testing123
Testing$
String should NOT match:
Test ing123
TESTING
Testing
TESTING1
testing1
testing$
I tried to modify this in several ways but I couldn't succeed in writing a proper regex to validate a space along with 3 combinations as i mentioned above.
Regex source: Regex for checking that at least 3 of 4 different character groups exist
UPDATED with Answer:
This one accepts any 3 or 4 conditions but no space.
^((?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])|(?=.*[a-z])(?=.*[A-Z])(?=.*[!@\#$%&/=?_.,:;\\-])|(?=.*[a-z])(?=.*[0-9])(?=.*[!@\#$%&/=?_.,:;\\-])|(?=.*[A-Z])(?=.*[0-9])(?=.*[!@\#$%&/=?_.,:;\\-]))\S{8,}$
\S
(i.e.,\S*$
). And get rid of the.*
at the beginning; that never belonged there. – Alan Moore Aug 24 '16 at 23:09