密码计验证 - RegEx

时间:2015-09-29 14:20:01

标签: regex validation passwords meter

密码计指导用户开发强密码。我想在仪表上实现以下密码要求。我已使用RegEx添加了下面突出显示的代码,但代码未检测到所需的密码准则。检测以下要求的正确代码是什么?

  • 8+个字符
  • 大写和小写字母
  • 特殊字符
  • 不得包含4个以上的连续字母
  • 不得包含4个连续数字

示例:
测试%401 =不足
2323Ejsdh! =不足 Tlv!897%=强大的 302 ^ PLs#= Strong

enter image description here enter image description here

enter image description here

if(val.length > 7 && val.match(/\d{4}/) && val.match(/[a-zA-Z]{4}/) && val.match(/[~,!,@,#,$,%,^,&,*,(,),+,-,?]/))

1 个答案:

答案 0 :(得分:1)

以下是针对您的任务的单正则表达式方法:

^(?=.*[a-zA-Z])(?=.*[^\w\s])(?!.*(?:[a-zA-Z]{4}|\d{4})).{8,}$

如果您需要匹配特定的“特殊”字符,请使用[~!@#$%^&*()+?-]类而不是[^\w\s]不是单词或空白字符)。 打破:

var rx = RegExp ("^" +                            // Start of string
                "(?=.*[a-zA-Z])" +               // Letter is required
                "(?=.*[^\\w\\s])" +              // A special character is required
                "(?!.*(?:[a-zA-Z]{4}|\\d{4}))" + // No 4 letters or digits on end are allowed
                ".{8,}" +                        // The whole length can be 8 or more characters
                "$"                              // End of string
              );

这是一个工作片段:

var ins = "Test%401";
var ins1 = "2323Ejsdh!";

var strong1 = "Tlv!897%";
var strong2 = "302^PLs#";

var rx = RegExp ("^" +                            // Start of string
                 "(?=.*[a-zA-Z])" +               // Letter is required
                 "(?=.*[^\\w\\s])" +              // A special character is required
                 "(?!.*(?:[a-zA-Z]{4}|\\d{4}))" + // No 4 letters or digits on end are allowed
                 ".{8,}" +                        // The whole length can be 8 or more characters
                 "$"                              // End of string
                );
document.getElementById("r").innerHTML += rx.test(ins) + "<br/>";
document.getElementById("r").innerHTML += rx.test(ins1) + "<br/>";
document.getElementById("r").innerHTML += rx.test(strong1) + "<br/>";
document.getElementById("r").innerHTML += rx.test(strong2) + "<br/>";
<div id="r"/>

另一种方法是分割条件:

function CheckPass(val) {
   if(val.length <= 7) {
       return "Password is too short!";
   }
   else if (/\d{4}/.test(val)) {
          return "Password contains 4 digits on end!";
   }
   else if (/[a-zA-Z]{4}/.test(val)) {
          return "Password contains 4 letters on end!";
   }
   else if (!/[~!@#$%^&*()+?-]/.test(val)) {
          return "Password must contain at least one special character from the \"~!@#$%^&*()+?-\" set!";
   }
   else if (!/[a-z]/i.test(val)) {
          return "Password must contain at least one letter!";
   }
       else
           return "Password is strong!";
}
    
document.getElementById("r").innerHTML +=  "<b>Test%401</b>: "+CheckPass("Test%401") + "<br/>";
document.getElementById("r").innerHTML +=  "<b>2323Ejsdh!</b>: "+CheckPass("2323Ejsdh!") + "<br/>";
document.getElementById("r").innerHTML +=  "<b>23!34%12!</b>: "+CheckPass("23!34%12!") + "<br/>";
document.getElementById("r").innerHTML +=  "<b>23w34W12D</b>: "+CheckPass("23w34W12D") + "<br/>";
document.getElementById("r").innerHTML += "<b>Tlv</b>: "+ CheckPass("Tlv") + "<br/>";
document.getElementById("r").innerHTML +=  "<b>Tlv!897%</b>: "+CheckPass("Tlv!897%") + "<br/>";
document.getElementById("r").innerHTML +=  "<b>302^PLs#</b>: "+ CheckPass("302^PLs#");
<div id="r"/>