简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM