1

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

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

示例:
Test%401 =
2323Ejsdh 不足!=
Tlv 不足!897% = 强
302^PLs# = 强

在此处输入图像描述 在此处输入图像描述

在此处输入图像描述

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

1 回答 1

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"/>

于 2015-10-01T07:30:16.140 回答