jquery – 密码强度不按规则工作

如何更新这个规则的下面的代码,我试过但似乎总是

当我进入T& 5(UppercaseSymbolNumber)时强大.

Rule:
at least 9 characters(Any)
two special characters
two numbers
two uppercase letters

There will be two strength type:
1) Week: If not fulfill all Rules.
Then System will not allow for process
2) Good strong password If
All rules fulfill – accept registration

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

最佳答案 T& 5匹配3个正则表达式 – 大写字母,数字和特殊字符 – 因此很强.如果需要检查两个大写字母,两个数字和两个特殊字符,请修改现有的正则表达式,如下所示:

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

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

另一点:你只检查通过时的长度>如果是的话,你只是增加了.您需要为字符串长度添加额外的检查,并在满足要求时传递增量.

看看下面的示例演示.

$(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>
点赞