Javascript 添加验证码验证 if (Contact.firstname.value == "") { alert("Please enter your first name"); return false; } if (Contact.secondname.value == "") { alert("Please enter your second name"); return false; } else if (Contact.emailaddress.value.indexOf('@') < 1 ) { alert("Please enter your email correctly"); return false; } if (Contact.comment.value == "") { alert("Please enter your message"); return false; } return true; } //--> </script>

Javascript 添加验证码验证 if (Contact.firstname.value == "") { alert("Please enter your first name"); return false; } if (Contact.secondname.value == "") { alert("Please enter your second name"); return false; } else if (Contact.emailaddress.value.indexOf('@') < 1 ) { alert("Please enter your email correctly"); return false; } if (Contact.comment.value == "") { alert("Please enter your message"); return false; } return true; } //--> </script>,javascript,php,validation,contact,Javascript,Php,Validation,Contact,我试图验证一个用PHP制作的附加验证码,但是我尝试的每件事都会把验证搞砸,根本无法验证。验证码将显示2个随机数和一个测试字段,用户可以将正确的数字添加到提供的总和中。我尝试了各种各样的方法,但所有的方法都阻止了表单的验证 <div class="Row"> <div class="Lable">First Name:</div> <!--End of Lable--> <div class="input"> <input type

我试图验证一个用PHP制作的附加验证码,但是我尝试的每件事都会把验证搞砸,根本无法验证。验证码将显示2个随机数和一个测试字段,用户可以将正确的数字添加到提供的总和中。我尝试了各种各样的方法,但所有的方法都阻止了表单的验证

<div class="Row">
<div class="Lable">First Name:</div> <!--End of Lable-->
<div class="input">
<input type="text" id="firstname" class="detail" name="firstname" placeholder="First Name"  />
</div> <!--End input-->
</div> <!--End row-->

<br />
<div class="Row">
<div class="Lable">Second Name:</div> <!--End of Lable-->
<div class="input">
<input type="text" id="secondname" class="detail" name="secondname" placeholder="Second Name"  />
</div> <!--End input-->
</div> <!--End row-->


<br />
<div class="Row">
<div class="Lable">Email Address:</div> <!--End of Lable-->
<div class="input">
<input type="text" id="emailaddress" class="detail" name="emailaddress" placeholder="Email Address" />
</div> <!--End input-->
</div> <!--End row-->

    <br />
<div class="Row">
<div class="Lable">Your Message:</div> <!--End of Lable-->
<div class="input">
<textarea id="comment" name="comment" class="mess" placeholder="Your Message" minlength="10"></textarea>
</div> <!--End input-->
</div> <!--End row--> 

<br />
<input id="number1" name="number1" readonly="readonly" class="Add" value="<?php echo rand(1,4) ?>" /> + 
<input id="number2" name="number2" readonly="readonly" class="Add" value="<?php echo rand(5,9) ?>" /> =
<input type="text" name="captcha" id="captcha" class="captcha" maxlength="2" />
<span id="spambot">(Are you human, or spambot?)</span>

<br />
<div class="submit">
<input type="submit" id="send" Name="send" value="Send"  />
</div><!--End of submit-->

<div class="Clear">
<input type="reset" id="clear" Name="Clear" value="Clear" />
</div>
if (Contact.firstname.value == "")
{
  alert("Please enter your first name");
  return false;
} 
   if (Contact.secondname.value == "")
{
  alert("Please enter your second name");
  return false;
} 


 else if (Contact.emailaddress.value.indexOf('@') < 1 )
 {  
   alert("Please enter your email correctly");
   return false;
 }
if (Contact.comment.value == "")
{
  alert("Please enter your message");
  return false;
} 



return true;
}

//-->
</script>


名字:

第二个名字:
电邮地址:
您的留言:

首先。。。这是一种糟糕的方法,因为从经验来看,不值得重新发明轮子

如果你有可能使用jQuery,我建议你使用一个验证插件,比如这个(我已经用了很多年了)

if (Contact.firstname.value == "")
{
  alert("Please enter your first name");
  return false;
} 
   if (Contact.secondname.value == "")
{
  alert("Please enter your second name");
  return false;
} 


 else if (Contact.emailaddress.value.indexOf('@') < 1 )
 {  
   alert("Please enter your email correctly");
   return false;
 }
if (Contact.comment.value == "")
{
  alert("Please enter your message");
  return false;
} 



return true;
}

//-->
</script>
它有相当好的文档,您可以扩展它,以包括您添加的数字(我知道我过去做过类似的事情)

if (Contact.firstname.value == "")
{
  alert("Please enter your first name");
  return false;
} 
   if (Contact.secondname.value == "")
{
  alert("Please enter your second name");
  return false;
} 


 else if (Contact.emailaddress.value.indexOf('@') < 1 )
 {  
   alert("Please enter your email correctly");
   return false;
 }
if (Contact.comment.value == "")
{
  alert("Please enter your message");
  return false;
} 



return true;
}

//-->
</script>
此外,它有一个相当大的用户群,你会发现这里有很多问题,他们的答案将帮助你找出你的问题

if (Contact.firstname.value == "")
{
  alert("Please enter your first name");
  return false;
} 
   if (Contact.secondname.value == "")
{
  alert("Please enter your second name");
  return false;
} 


 else if (Contact.emailaddress.value.indexOf('@') < 1 )
 {  
   alert("Please enter your email correctly");
   return false;
 }
if (Contact.comment.value == "")
{
  alert("Please enter your message");
  return false;
} 



return true;
}

//-->
</script>
++您可以选择在表单字段旁边显示每个错误,或者向用户发出警报()

客户端(javascript)验证对用户来说很方便,可以将错误保存到服务器的往返路径;必须在服务器上验证验证码——发布机器人可能根本不会运行您的javascript。
if (Contact.firstname.value == "")
{
  alert("Please enter your first name");
  return false;
} 
   if (Contact.secondname.value == "")
{
  alert("Please enter your second name");
  return false;
} 


 else if (Contact.emailaddress.value.indexOf('@') < 1 )
 {  
   alert("Please enter your email correctly");
   return false;
 }
if (Contact.comment.value == "")
{
  alert("Please enter your message");
  return false;
} 



return true;
}

//-->
</script>