简体   繁体   English

JS 验证失败,但提交表单

[英]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.我尝试将 onsubmit 移至 onclick 并将其放入输入中,但仍然没有。 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]给你一个元素,而不是他的值。

document.getElementsByName("schoolInput")[0].value is what you need. document.getElementsByName("schoolInput")[0].value是您所需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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