简体   繁体   English

JavaScript表单验证仍会重定向

[英]Javascript form validation still redirects

For some reason, this code always redirects to education.php regardless of whether or not the fields are blank. 由于某些原因,无论这些字段是否为空白,此代码始终都会重定向到education.php。 I want to verify the fields have values in them, but for some reason they keep redirecting but not writing anything in the database. 我想验证字段中是否包含值,但由于某些原因,它们会继续重定向,但不会在数据库中写入任何内容。 Any help would be greatly appreciated. 任何帮助将不胜感激。

<body>
<form action="education.php" method="post" onsubmit="return validate_fields()">
<div style="text-align: right">
<ul>
First Name: <input id="first_name" name="first_name" size=25/> <br>
Last Name: <input id="last_name" name="last_name" size=25/> <br>
Email: <input id="emailaddress" name="emailaddress" size=25/> <br>
Password: <input id="user_password" name="user_password" type="password" size=25/> <br>
<center><input type="submit" name="submit" id="submit" value="Register Now"/></center>
</ul>
</div>
</form>
<script type="text/javascript">
function validate_fields()
{
    var first_name = document.getElementByID("first_name").value;
    var last_name = document.getElementById("last_name").value;
    alert(""+first_name+" "+last_name);
    if (first_name.length < 1 || last_name.length < 1)
    {
        alert("Please fill in your name.");
        return false;
    }
    var email = document.getElementById("emailaddress").value;
    if (email.length < 1)
    {
        alert("Please fill in your email address.");
        return false;
    }
    var password = document.getElementById("password").value;
    if (email.length < 1)
    {
        alert("Please put in a password.");
        return false;
    }
    alert(first_name+" "+last_name+" "+email);
    return false; //was true, changed to see if still redirects.
}
</script>
</body>

You've got a typo on the first line of your function so it isn't returning false (or true), it is just not running at all. 您在函数的第一行上有一个错字,因此它不会返回false(或true),根本就没有运行。 This explains both why you don't get any of the alerts and why the form submit goes ahead. 这解释了为什么您没有收到任何警报以及为什么表单提交继续进行。

var first_name = document.getElementByID("first_name").value;
// you need a lowercase "d" here ------^

It should be .getElementById() , not .getElementByID() . 它应该是.getElementById() ,而不是.getElementByID()

This is the sort of thing you can easily find for yourself with the appropriate developer tools for your browser. 使用适合您浏览器的开发人员工具,您可以轻松地自己找到这种东西。 Chrome has this built in (just press ctrl-shift-J to bring up dev tools), or you can add Firebug for FireFox, and IE has had a dev toolbar option for several versions now. Chrome具有内置功能(只需按ctrl-shift-J即可打开开发工具),或者您可以添加Firebug for FireFox,并且IE现在具有适用于多个版本的开发工具栏选项。

You have a typo here: 您在这里有错字:

var first_name = document.getElementById("first_name").value;

You wrote ByID it sould be ById ! 您将ByID编写为ById

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

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