dounou9751 2014-04-11 16:18
浏览 85
已采纳

添加验证码验证

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. The captcha will display 2 random numbers and a test field that the user can add the correct number to the sum provided. 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

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.

展开全部

  • 写回答

1条回答 默认 最新

  • dqtu14636 2014-04-11 16:28
    关注

    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/

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 KeiI中头文件找不到怎么解决
  • ¥15 QT6将音频采样数据转PCM
  • ¥15 本地安装org.Hs.eg.dby一直这样的图片报错如何解决?
  • ¥15 下面三个文件分别是OFDM波形的数据,我的思路公式和我写的成像算法代码,有没有人能帮我改一改,如何解决?
  • ¥15 Ubuntu打开gazebo模型调不出来,如何解决?
  • ¥100 有chang请一位会arm和dsp的朋友解读一个工程
  • ¥50 求代做一个阿里云百炼的小实验
  • ¥15 查询优化:A表100000行,B表2000 行,内存页大小只有20页,运行时3页,设计两个表等值连接的最简单的算法
  • ¥15 led数码显示控制(标签-流程图)
  • ¥20 为什么在复位后出现错误帧