vendredi 4 mai 2018

Switch jumps to case 2: [duplicate]

This question already has an answer here:

I'm trying to make a JS function that checks the security of the users' password and changes the color of the password accordingly.

So far I've figured out that my switch jumps to case 2: when strength = 0 and it jumps to case 3: when strength = 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;
    }
}

Aucun commentaire:

Enregistrer un commentaire