javascript - Regex for combination of given rules -


i'm trying write regex validate password given rule.

passwords must @ least 8 characters in length , contain @ least 3 of following 4 types of characters:

  • lower case letters (i.e. a-z)
  • upper case letters (i.e. a-z)
  • numbers (i.e. 0-9)
  • special characters (e.g. !@#$&*)

i going through this discussion , found this great answer on there.

now i'm trying write regex mentioned requirements , came solution this

^(?=.*[a-z])(?=.*[!@#$&*])(?=.*[0-9])(?=.*[a-z]).{8,}| (?=.*[!@#$&*])(?=.*[0-9])(?=.*[a-z]).{8,}| (?=.*[a-z])(?=.*[0-9])(?=.*[a-z]).{8,}| (?=.*[a-z])(?=.*[!@#$&*])(?=.*[a-z]).{8,}| (?=.*[a-z])(?=.*[!@#$&*])(?=.*[0-9]).{8,}$ 

and working perfect see rubular want optimize these regex , i'm not sure if there way simplify this. suggestion appreciated. many thanks

do (and work on app in future) favour, , split regexp in 4:

{   :lowercase => /regex_for_lowercase/,   :uppercase => /regex_for_uppercase/,   :digits => /regex_for_digits/,   :symbols => /regex_for_symbols/, } 

then count how many of these 4 rules password matches. give chance show more helpful error message if entered password not validate.


Comments