简体   繁体   English

HTML表单JS验证

[英]HTML Form JS Validation

I'm trying to validate my login form so that the field will have a min/max length before the values being posted to my PHP file. 我正在尝试验证我的登录表单,以便在将值发布到我的PHP文件之前,该字段具有最小/最大长度。

JavaScript: JavaScript的:

function loginValidation() {
    var user = document.login.username.value;
    var pass = document.login.password.value;

    if (user.length <= 6 || user.length > 20) {
        alert("Please make sure the username is between 7 and 20 characters.");
        return false;
    }

    if (pass.length <= 8 || pass.length > 50) {
        alert("Please make sure the password field is between 9 and 50 characters.");
        return false;
    }
}

HTML: HTML:

<form name="login" onsubmit="loginValidation()" action="login.php" method="post">
    <div class="form-group row">
        <label for="username" class="lead para col-sm-4">Username</label>
        <input type="text" class="form-control transparent col-sm" id="username" name="username" placeholder="Username">
    </div>
    <div class="form-group row">
        <label for="password" class="lead para col-sm-4">Password</label>
        <input type="password" class="form-control col-sm" id="password" name="password" placeholder="Password">
    </div>
    <button type="submit" class="btn btn-outline-primary">LOGIN</button>
</form>

It seems that the code is ignoring the Function or that it just doesn't work (errors maybe?). 似乎代码忽略了该功能或它不起作用(可能是错误?)。

Other information that might be useful to know is that the JavaScript file with all my functions is external. 其他可能有用的信息是具有我所有功能的JavaScript文件是外部的。

<script src="/JS/functions.js"></script>

Working fiddle . 工作提琴

You are just missing the return in the function call in your onsubmit , should be like : 您只是在onsubmit缺少函数调用中的return ,应该像这样:

<form name="login" onsubmit="return loginValidation()" action="login.php" method="post">
_____________________________^^^^^^

It will be better to attach the event in your JS part : 最好将事件附加到您的JS部分中:

document.querySelector('form[name="login"]').addEventListener("submit", loginValidation);

 function loginValidation() { var user = document.login.username.value; var pass = document.login.password.value; if (user.length <= 6 || user.length > 20) { alert("Please make sure the username is between 7 and 20 characters."); return false; } if (pass.length <= 8 || pass.length > 50) { alert("Please make sure the password field is between 9 and 50 characters."); return false; } return true; } 
 <form name="login" onsubmit="return loginValidation()" action="login.php" method="post"> <div class="form-group row"> <label for="username" class="lead para col-sm-4">Username</label> <input type="text" class="form-control transparent col-sm" id="username" name="username" placeholder="Username"> </div> <div class="form-group row"> <label for="password" class="lead para col-sm-4">Password</label> <input type="password" class="form-control col-sm" id="password" name="password" placeholder="Password"> </div> <button type="submit" class="btn btn-outline-primary">LOGIN</button> </form> 

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

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