切换跳转到案例2:

时间:2018-05-04 13:02:15

标签: javascript if-statement switch-statement

我正在尝试创建一个JS函数来检查用户密码的安全性并相应地更改密码的颜色。

到目前为止,我已经发现当强度= 0 时,我的开关会跳转到案例2 ,然后跳转到案例3:强度= 1

function passwordStrength() {
    var password = String(document.getElementById("signupScreenPasswordField"));
    var passwordField = document.getElementById("signupScreenPasswordField");
    var passwordRepeatField = document.getElementById("signupScreenPasswordRepeatField");
    let strength = 0;

    if  (password.match(/[a-zA-Z0-9][a-zA-Z0-9]+/)) {
        strength += 1;
    }
    if (password.match(/[!?@£$%^&*()~<>]+/)) {
        strength += 1;
    }
    if (password.length > 6) {
        strength += 1;
    }

    switch(strength) {
        // very weak password
        case 0:
            passwordField.style.color = "red";
            passwordRepeatField.style.color = "red";
            break;
        // weak password
        case 1:
            passwordField.style.color = "orange";
            passwordRepeatField.style.color = "orange";
            break;
        // strong password
        case 2:
            passwordField.style.color = "yellow";
            passwordRepeatField.style.color = "yellow";
            break;
        // very strong password
        case 3:
            passwordField.style.color = "green";
            passwordRepeatField.style.color = "green";
            break;
    }
}

0 个答案:

没有答案