Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for Password validation in Javascript

Regex Password complexity requires that any three of the following four characteristics must be applied when creating or changing a password.

  • Alpha characters - at least 1 upper case alpha character
  • Alpha characters - at least 1 lower case alpha character
  • Numeric characters - at least 1 numeric character
  • Special characters - at least 1 special character

I am trying with the following code, but its not working for special characters

(?=^.{6,}$)((?=.*\d)(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[^A-Za-z0-9])(?=.*[a-z])|(?=.*[^A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[A-Z])(?=.*[^A-Za-z0-9]))^.*

I want my regex to be validated against the following 4 cases

Match cases

  • P@ssword
  • Password1
  • p@ssword1
  • p@12345
like image 505
sivanv Avatar asked Jun 14 '13 07:06

sivanv


1 Answers

I think that a regex you can use is:

(?=^.{6,}$)(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[^A-Za-z0-9]).*

I'm not sure why you have so many or operators in your regex but this one matches if:

  • (?=^.{6,}$) - String is > 5 chars
  • (?=.*[0-9]) - Contains a digit
  • (?=.*[A-Z]) - Contains an uppercase letter
  • (?=.*[a-z]) - Contains a lowercase letter
  • (?=.*[^A-Za-z0-9]) - A character not being alphanumeric.

Regular expression image

like image 171
Jerry Avatar answered Sep 29 '22 07:09

Jerry