使用javascript/jquery检查密码强度

Check password strength using javascript / jquery

本文关键字:密码 检查 jquery javascript 使用      更新时间:2023-09-26

我想在我的asp.net网站上为我的文本框实现一个密码强度功能。

目前,我的代码如下:

<span id="password_strength"></span>
<script type="text/javascript">
    function CheckPasswordStrength(password) {
        var password_strength = document.getElementById("password_strength");
        //if textBox is empty
        if(password.length==0){
            password_strength.innerHTML = "";
            return;
        }
        //Regular Expressions
        var regex = new Array();
        regex.push("[A-Z]"); //For Uppercase Alphabet
        regex.push("[a-z]"); //For Lowercase Alphabet
        regex.push("[0-9]"); //For Numeric Digits
        regex.push("[$@$!%*#?&]"); //For Special Characters
        var passed = 0;
        //Validation for each Regular Expression
        for (var i = 0; i < regex.length; i++) {
            if(new RegExp (regex[i]).test(password){
                passed++;
            }
        }
        //Validation for Length of Password
        if(passed > 2 && password.length > 8){
            passed++;
        }
        //Display of Status
        var color = "";
        var passwordStrength = "";
        switch(passed){
            case 0:
            case 1:
                passwordStrength = "Password is Weak.";
                color = "Red";
                break;
            case 2:
                passwordStrength = "Password is Good.";
                color = "darkorange";
                break;
            case 3:
            case 4:
                passwordStrength = "Password is Strong.";
                color = "Green";
                break;
            case 5:
                passwordStrength = "Password is Very Strong.";
                color = "darkgreen";
                break;
        }
        password_strength.innerHTML = passwordStrength;
        password_strength.style.color = color;
    }
</script>
<div class="row">
    <div class="col-sm-6">
        <center><asp:label runat="server" text="Password :" Font-Bold="True" Font-Italic="False"></asp:label></center>
        <center><asp:TextBox ID="tbPassword" runat="server" onkeyup="CheckPasswordStrength(this.value)"></asp:TextBox></center>
    </div>
</div>

我在网上找到了这个代码。我在我的网站上试过了,但它不起作用。如果有人能帮我写代码,我将不胜感激。

break添加到case 3case 0中。然后在case 5之后设置default

http://www.w3schools.com/js/js_switch.asp

编辑:

我更新了你的功能

 function CheckPasswordStrength(password) {
  var password_strength = document.getElementById("password_strength");

    //if textBox is empty
    if(password.length==0){
        password_strength.innerHTML = "";
        return;
    }
    //Regular Expressions
    var regex = new Array();
    regex.push("[A-Z]"); //For Uppercase Alphabet
    regex.push("[a-z]"); //For Lowercase Alphabet
    regex.push("[0-9]"); //For Numeric Digits
    regex.push("[$@$!%*#?&]"); //For Special Characters
    var passed = 0;
    //Validation for each Regular Expression
    for (var i = 0; i < regex.length; i++) {
        if((new RegExp (regex[i])).test(password)){
            passed++;
        }
    }
    //Validation for Length of Password
    if(passed > 2 && password.length > 8){
        passed++;
    }
    //Display of Status
    var color = "";
    var passwordStrength = "";
    switch(passed){
        case 0:
            break;
        case 1:
            passwordStrength = "Password is Weak.";
            color = "Red";
            break;
        case 2:
            passwordStrength = "Password is Good.";
            color = "darkorange";
            break;
        case 3:
                break;
        case 4:
            passwordStrength = "Password is Strong.";
            color = "Green";
            break;
        case 5:
            passwordStrength = "Password is Very Strong.";
            color = "darkgreen";
            break;
    }
    password_strength.innerHTML = passwordStrength;
    password_strength.style.color = color;
}

测试:https://jsfiddle.net/Lko29h8m/1/