2

My password validation criteria is as follows:

  • Must contain at least two lower case characters
  • Must contain at least two upper case characters
  • Must contain at least two numeric characters
  • Must contain at least two special characters i.e. @#$%
  • Must be at least 11 characters long

I tried using this for the first four criteria:

/(?:\d{2,})(?:[a-z]{2,})(?:[A-Z]{2,})(?:[!"'#$&()*+-@,.:;<>=?^_`{|}~\/\\]{2,})/g

But it does not match the following string which i would expect it to:

12QAqa@#

But it does match:

12qaQA@#

The order that the validation criteria is not important. How do i rewrite the regex to not take into account the order?

RyanP13
  • 7,413
  • 27
  • 96
  • 166

1 Answers1

1

The following seems to meet all your requirements:

/*
Must contain at least two lower case characters
Must contain at least two upper case characters
Must contain at least two numeric characters
Must contain at least two special characters i.e. @#$%
Must be at least 11 characters long
*/

var password ='12qaQ@#123456789A';
var pattern  =/^(?=(.*[a-z]){2,})(?=(.*[A-Z]){2,})(?=(.*[0-9]){2,})(?=(.*[!@#\$%]){2,}).{11,}$/;

alert( pattern.test(password) ); 

https://jsfiddle.net/rryg67v1/1/

^                     // start of line
(?=(.*[a-z]){2,})     // look ahead and make sure at least two lower case characters exist
(?=(.*[A-Z]){2,})     // look ahead and make sure at least two upper case characters exist
(?=(.*[0-9]){2,})     // look ahead and make sure at least two numbers exist
(?=(.*[!@#\$%]){2,})  // look ahead and make sure at least two special characters exist
.{11,}                // match at least 11 characters
$                     // end of line            

Good luck!!

angelcool.net
  • 2,505
  • 1
  • 24
  • 26