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;

if (valid) {
    alert("Logging in...");
    window.location.href = "";
    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.

Browse More Popular Posts

Leave a Comment