0

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;
    }
}
  • If `strength` is 0, then it would be going to `case 0`. What makes you think that it's 0? – JLRishe May 04 '18 at 13:06
  • 4
    `var password = String(document....)` no no no, it's `document.getElementById("signupScreenPasswordField").value` instead. However, what you have with `String(...)` would make a very strong password. – baao May 04 '18 at 13:06
  • 1
    @baao post it as an answer. – Doruk May 04 '18 at 13:08
  • Nah, I already closed the question as a duplicate @Doruk – baao May 04 '18 at 13:08
  • To explain more detailed: Using `String()` instead of getting the value always sets the password to the string `[object HTMLInputElement]` instead of what the user entered. That string will always contain letters and will always be longer than 6 characters, hence passing the 1st and 3rd IF. – Shilly May 04 '18 at 13:17

0 Answers0