jQuery表单验证equalTo隐藏字段

时间:2013-02-16 12:32:41

标签: php jquery captcha hidden-field

我正在处理一个在提交之前使用jQuery验证的表单。其中一个领域是验证码。

这个想法非常简单,验证码显示为图像,也存储在隐藏字段中。当用户在输入字段中输入验证码时,它应该等于隐藏字段的值。这是我的jQuery代码;

<script>
$(document).ready(function(){
 $("#register").validate({ 
errorElement: 'div', ignore: 'hidden',
rules: {
          password: { 
                required: true
          }, 
          password2: { 
                required: true, equalTo: "#password"
          },
          agree: {
              required: true
          },
          captcha: {
              required: true, equalTo: "#captcha2"
          }
      },
messages: {
password2: "Please repeat the same password",
captcha: "The captcha you entered is wrong",
agree: "You have to be at least 16 years old and agree to the terms of service" 
}
});
});
</script>

表单的html代码更简单,但我只会显示其中的一部分。

<p>
    <img src='/includes/captcha.php'  />
</p>
<p>
  <label for="Captcha">Captcha</label>
    <em> *</em><input type="text" name="captcha" id="captcha" size="25" 
   class="require"   minlength="5" />
<input type="hidden" name="captcha2" id="captcha2" 
 value="<?php echo    $_SESSION['captcha'];?>" />

这应该有效。但问题是,我不断收到我在jQuery代码中设置的错误“你输入的验证码是错误的”。 任何人都可以告诉我为什么代码不起作用?

BTW,我检查了隐藏字段的值,它与验证码相同,因此可以正常工作

更新: 好的,这里有些奇怪。当我回显会话['captcha']时,我获得的值与隐藏字段中的值不同。当我回应它时,我得到了之前的验证码。所以我们说验证码值是abc。当我刷新验证码+隐藏字段更改为def。但是当我回应会话['验证码']时,我仍然得到了abc。

0 个答案:

没有答案
相关问题