到目前爲止,我一直在使用下面的表單驗證。它需要字段,電子郵件驗證和垃圾郵件蜜罐。但是,當我真的不應該提交表單時,我的一個客戶正在獲得空白表單結果。所以也許我沒有看到明顯的。這是非常簡單的代碼,我認爲。任何人都可以快速查看,並讓我知道,如果我錯過了什麼?表單驗證和蜜罐 - 我的代碼是否正確?
另一方面,垃圾郵件機器人比蜜罐更聰明嗎?
這是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>
表單結果全部都是空白,包括電子郵件字段,它告訴我代碼中的某些內容不正確,或者機器人現在變得更加智能並且有辦法驗證。 根據我的代碼,如果名字和姓氏是空白的表單不應該通過,對不對? – Julesfrog 2011-12-29 15:58:00
空白我的意思是(32)。但是,如果電子郵件也是空白的,我的懷疑是錯誤的。 – 2011-12-29 16:04:34
感謝您的輸入Eugen。 – Julesfrog 2011-12-29 16:10:49