到目前为止,我一直在成功使用下面的表单验证。它具有必填字段、电子邮件验证和垃圾邮件蜜罐。但是,我的一位客户在根本不应该提交表单时得到空白表单结果。所以也许我没有看到明显的东西。我认为这是非常简单的代码。任何人都可以快速查看并让我知道我是否遗漏了什么?
另一方面,垃圾邮件机器人是否比蜜罐更智能?
这是JS:
<script>
function verify() {
var themessage = "You are required to complete the following fields: ";
var x=document.form.email.value
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (document.form.address.value!="") {
themessage = "You are not human! No form for you!";
}
if (document.form.first_name.value=="") {
themessage = themessage + " - First Name";
}
if (document.form.last_name.value=="") {
themessage = themessage + " - Last Name";
}
if (document.form.email.value=="") {
themessage = themessage + " - E-mail";
}
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) {
themessage = "You need to enter a valid email address";
}
//alert if fields are empty and cancel form submit
if (themessage == "You are required to complete the following fields: ") {
document.form.submit();
}
else {
alert(themessage);
return false;
}
}
</script>
和 HTML:
<form name="form" method="post" action="output.php">
<div id="input">
<div id="field">First Name:</div>
<input name="first_name" type="text" id="first_name">
</div>
<div id="input">
<div id="field">Last Name:</div>
<input name="last_name" type="text" id="last_name">
</div>
<div id="input">
<div id="field">Email:</div>
<input name="email" type="text" id="email">
</div>
<div class="input address"><!-- This is the Honeypot -->
<div id="field">Address:</div>
<input name="address" type="text" id="address">
</div>
<div id="input">
<div id="field">Phone:</div>
<input name="phone" type="text" id="phone">
</div>
<div id="input">
<div id="field3">Comments:</div>
<textarea name="comments" id="comments"></textarea>
</div>
<input type="button" value="Submit" onclick="verify();">
</form>