密码强度不按规则工作

时间:2016-06-21 08:09:05

标签: jquery regex

如何更新此规则的下面代码,我试过但似乎总是如此 当我进入T& 5(UppercaseSymbolNumber)时强大。

  

规则:
  至少9个字符(任意)
  两个特殊字符
  两个数字
  两个大写字母

     

将有两种力量类型:
  1)周:如果不符合所有规则   然后系统将不允许进程
  2)良好的强密码如果
  所有规则都符合 - 接受注册

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <style type="text/css">
            body
            {
                font-family: Arial;
                font-size: 10pt;
            }
            #password_strength
            {
                font-weight: bold;
            }
        </style>
    </head>
    <body>
        <input type="text" id="txtPassword" />
        <span id="password_strength"></span>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script type="text/javascript">
            $(function () { 
                $("#txtPassword").bind("keyup", function () {
                    //TextBox left blank.
                    if ($(this).val().length == 0) {
                        $("#password_strength").html("");
                        return;
                    }

                    //Regular Expressions.
                    var regex = new Array();
                    regex.push("[A-Z]"); //Uppercase Alphabet.
                    regex.push("[^A-Z$@$!%*#?&0-9]"); //Lowercase Alphabet.
                    regex.push("[0-9]"); //Digit.
                    regex.push("[$@$!%*#?&]"); //Special Character.

                    var passed = 0;

                    //Validate for each Regular Expression.
                    for (var i = 0; i < regex.length; i++) {
                        if (new RegExp(regex[i]).test($(this).val())) {
                            passed++;
                        }
                    }

                    //Validate for length of Password.
                    if (passed > 2 && $(this).val().length >= 9) {
                        passed++;
                    }

                    //Display status.
                    var color = "";
                    var strength = "";
                    switch (passed) {
                        case 0:
                        case 1:
                            strength = "Weak";
                            color = "red";
                            break;
                        case 2:
                            strength = "Good";
                            color = "darkorange";
                            break;
                        case 3:
                        case 4:
                            strength = "Strong";
                            color = "green";
                            break;
                        case 5:
                            strength = "Very Strong";
                            color = "darkgreen";
                            break;
                    }
                    $("#password_strength").html(strength);
                    $("#password_strength").css("color", color);
                });
            });
        </script>
    </body>
</html>

3 个答案:

答案 0 :(得分:1)

T&5匹配3个正则表达式 - 大写字母数字特殊字符 - 因此。如果您需要检查两个大写字母,两个数字和两个特殊字符,请修改现有的正则表达式,如下所示:

regex.push("[A-Z][^A-Z]*[A-Z]");
regex.push("[0-9][^0-9]*[0-9]");
regex.push("[@$!%*#?&][^@$!%*#?&]*[@$!%*#?&]");

这些基本上测试你想要的字符,然后是你需要的任何0+字符(用带有*量词的否定字符类等价(反义词类)定义),然后第二次出现所需字符的匹配与初始字符类的另一个实例匹配。

另一点:您只有在passed&gt;时检查长度? 2 即可。如果是,您只需增加passed。您需要为字符串长度添加额外的检查,如果满足要求,则增加passed

查看下面的示例演示。

$(function() {
  $("#txtPassword").bind("keyup", function() {
    //TextBox left blank.
    var text = $(this).val();
    if (text.length == 0) {
      $("#password_strength").html("");
      return;
    }

    //Regular Expressions.
    var regex = new Array();
    regex.push("[A-Z][^A-Z]*[A-Z]"); //Uppercase Alphabet.
    regex.push("[^A-Z$@$!%*#?&0-9]"); //Lowercase Alphabet.
    regex.push("[0-9][^0-9]*[0-9]"); //Digit.
    regex.push("[@$!%*#?&][^@$!%*#?&]*[@$!%*#?&]"); //Special Character.
    var passed = 0;

    //Validate for each Regular Expression.
    for (var i = 0; i < regex.length; i++) {
      if (new RegExp(regex[i]).test(text)) {
        console.log(text + " passed: " + regex[i]); // Console!
        passed++;                                   // Increment on match
      }
    }
    
    //Validate for length of Password.
    if (text.length >= 9) {
         console.log(text + " passed length check"); // Console!
    	 passed++;                                   // Increment if length is OK
    }
    
    //Display status.
    var color = "";
    var strength = "";
    switch (passed) {
      case 0:
      case 1:
        strength = "Weak";
        color = "red";
        break;
      case 2:
        strength = "Good";
        color = "darkorange";
        break;
      case 3:
      case 4:
        strength = "Strong";
        color = "green";
        break;
      case 5:
        strength = "Very Strong";
        color = "darkgreen";
        break;
    }
    $("#password_strength").html(strength);
    $("#password_strength").css("color", color);
  });
});
body {
  font-family: Arial;
  font-size: 10pt;
}

#password_strength {
  font-weight: bold;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="txtPassword" />
<span id="password_strength"></span>

答案 1 :(得分:0)

这是正则表达式

/^(?=.*[A-Z].*[A-Z])(?=.*[!@#$&*].*[!@#$&*])(?=.*[0-9].*[0-9]).{9,}$/

解释

^                         Start anchor
(?=.*[A-Z].*[A-Z])        Ensure string has two uppercase letters.
(?=.*[!@#$&*].*[!@#$&*])  Ensure string has two special case letter.
(?=.*[0-9].*[0-9])        Ensure string has two digits.
.{9,}                     Ensure string is of atleast length 9.
$                         End anchor.

答案 2 :(得分:0)

<div ng-repeat="role in user.roles">             
    <h2>{{role.id}}</h2>
</div>