使用javascript / jquery检查密码强度

时间:2016-08-05 01:26:16

标签: javascript jquery

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

目前,我的代码如下所示:

sorted(["abcd","abcdef","def","abcdef"], key=len)

网上有这个代码......但是在我的网站上尝试过它并没有用。 如果有人可以帮我处理我的代码,我真的很感激。

谢谢。 :)

1 个答案:

答案 0 :(得分:0)

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

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/