Regex for password validation using JavaScript

I need a regex for password validation using JavaScript which should contain at least one numeric and one special character symbol in (* ^ % $ # @ ) and more than one alpha characters. Currently I am using below code snippet, which is not working:

/^(?=(.*[a-zA-Z]{2}))(?=.*d)(?=.*(*))(?=.*(^))(?=.*(%))(?=.*(#))(?=.*($))(?=.*(@))$/

Can somebody help me out?

Answer

Line up all your requirements as assertions, then match the string
if the assertions are satisfied.
(?i)^(?=(?:.*[a-z]){2})(?=.*d)(?=.*[*^%$#@]).+$

 (?i)                          # In line modifier - case insensitive
 ^                             # BOS
 (?=                           # Assert - at least 2 alpha's
      (?: .* [a-z] ){2}
 )
 (?= .* d )                   # Assert - at least 1 numeric
 (?= .* [*^%$#@] )             # Assert - at least 1 special

 .+                            # Match all here
 $                             # EOS

Leave a Reply

Your email address will not be published. Required fields are marked *