简体   繁体   English

JavaScript 密码验证正则表达式按分组顺序失败

[英]JavaScript password validation regex failing on order of groupings

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 ie @#$%必须至少包含两个特殊字符,即@#$%
  • Must be at least 11 characters long长度必须至少为 11 个字符

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?我如何重写正则表达式以不考虑顺序?

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/ 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!!祝你好运!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM