[Solved] javascript “if” statement returning false value when both values are equal


You are breaking the loop and your if(valid) code is within the for loop. You probably meant to have the if(valid) outside the for loop scope.

For example you can do:

valid = false;

for (var i = 0; i < usernameArray.length; i++) {
    if ((un == usernameArray[i]) && (pw == passwordArray[i])) {
        valid = true;
        break;
    }
}

if (valid) {
    alert("Logging in...");
    window.location.href = "http://www.google.com";
    return false;
}

Notice I closed the for loop.

Also notice you have an valid=true after the if statement. I assume you did it for debugging purposes, but make sure to remove it.

4

solved javascript “if” statement returning false value when both values are equal