JeremyRenner2
JeremyRenner2

Reputation: 61

JS Validation Failing, But Form Submitting

I have the following form

<form action="login.php" method="post" onsubmit="return validateLogin()">
    Username: <input type="text" autocomplete="off" name="usernameInput"><br>
    Password: <input type="password" autocomplete="off" name="passwordInput"><br>
    School:      <input type="text" name="schoolInput" autocomplete="off"><div id="erschool"></div>
    <input type="submit" name="loginSubmit" value="login">
</form>

With the following validation

function validateLogin() {
    var passed = true;
    if((document.getElementsByName("schoolInput")[0] == "") || (document.getElementsByName("usernameInput")[0] == "") || (document.getElementsByName("passwordInput")[0] == "") ){
        document.getElementById("erschool").innerHTML = "Complete all fields";
        passed = false;
    }
    return passed;
}

I have tried moving the onsubmit to onclick and putting it in the input but still nothing. The form gets submitted and the validation does nothing.

Help

Upvotes: 0

Views: 24

Answers (1)

farvilain
farvilain

Reputation: 2562

document.getElementsByName("schoolInput")[0] gives you an element, not his value.

document.getElementsByName("schoolInput")[0].value is what you need.

Upvotes: 1

Related Questions