简体   繁体   English

附加验证码验证

[英]Addition captcha validation

Im trying to validate an addition captcha made with PHP but everything I try just messes up the validation and doesn't validate at all. 我试图验证由PHP制作的附加验证码,但我尝试的所有操作都弄乱了验证,并且根本没有进行验证。 The captcha will display 2 random numbers and a test field that the user can add the correct number to the sum provided. 验证码将显示2个随机数和一个测试字段,用户可以将正确的数字添加到所提供的总和中。 I have tried various thing but all of the just stop the form from validating at all. 我已经尝试过各种方法,但是所有这些都无法阻止表单进行验证。

<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>

JavaScript validation 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>

I know that there is no validation attempt made on here but please believe me that I have give it a shot before posting but had no luck. 我知道这里没有进行验证的尝试,但是请相信我,我在发布之前已经尝试了一下,但是没有运气。

First of all... this is a kind of bad approach as, speaking from experience, it is not worth to reinvent the wheel! 首先...这是一种不好的方法,因为从经验上来讲,不值得重新发明轮子!

If you have the posibility to use jQuery i'd advise a validation plugin, like this one (i've used it for years) http://jqueryvalidation.org/ 如果您有使用jQuery的可能,我建议您提供一个验证插件,例如这样的插件(我已经使用了多年) http://jqueryvalidation.org/

It has pretty good docs and you'll be able to extend it to include your addition of numbers (i know i've done something like that in the past)! 它具有很好的文档,您可以扩展它以包括数字的加法(我知道我过去做过类似的事情)!

Also, it has a pretty large user base and you will find many questions asked here, whose answers will help you figure out your issues! 另外,它有很大的用户群,您会在这里发现许多问题,这些问题的答案将帮助您解决问题!

++You have the option to either display each error next to the form fields, or alert() the errors to the user ++您可以选择在表单字段旁边显示每个错误,或将错误提示给用户

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

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