简体   繁体   English

将ReCaptcha与jQuery Validate结合使用,可给出正确的响应,但会产生错误

[英]Using ReCaptcha with jQuery Validate, giving correct response but gives error

I'm using jQuery validate with a registration form for my website with a reCaptcha at the bottom. 我使用的是jQuery validate和我的网站的注册表格,底部是reCaptcha。 Here is the script to check the form for errors (edited to only the relevant parts): 这是检查表单是否有错误的脚本(仅编辑为相关部分):

$(document).ready(function() { 
  jQuery.validator.addMethod("checkCaptcha", function() {
    var phpquery = $.ajax({url:"verify.php",
      type: "POST",
      async: false,
      data:{recaptcha_challenge_field:Recaptcha.get_challenge(),recaptcha_response_field:Recaptcha.get_response()},
      success:function(resp) {
        if (resp == 'false') {
          console.dir(resp);
          return false;
        } else {
          console.dir(resp);
          return true;
        }
      }
    });
  },"");

  $('#regForm').validate({
    rules:{
      recaptcha_response_field:{required:true,checkCaptcha:true}
    },
    messages:{
      recaptcha_response_field:{checkCaptcha:"Your Captcha response was incorrect. Please try again."}
    }
  });
});

What happens is when I enter the correct reCaptcha response and either click submit or tab out of the response text field, it will initially say true in the console, then immediately throw false and prevent me from submitting the form. 当我输入正确的reCaptcha响应并单击“响应”文本字段中的“提交”或“制表符”时,会发生什么情况,当它最初在控制台中显示为true ,然后立即抛出false并阻止我提交表单。

What also happens is when the incorrect reCaptcha response is entered, it will throw false like it's supposed to but for every letter I type in, it will submit that to verify.php and return false . 当输入了错误的reCaptcha响应时,也会发生错误,如预期那样,它将抛出false ,但是对于我输入的每个字母,它将提交到verify.php并返回false When I finally get done typing in the correct reCaptcha, it doesn't recognize it as such and still returns false . 当我最终完成输入正确的reCaptcha的输入时,它无法识别出该false ,并且仍然返回false

Reloading the reCaptcha also throws a false , even when the correct response is entered. 即使输入正确的响应,重新加载reCaptcha也会抛出false

I know the verify.php works correctly, because when the <form> would have action="verify.php," everything would work perfectly (ie, tell you if you entered the correct reCaptcha, regardless of how many times you tried). 我知道verify.php可以正常工作,因为当<form>具有action="verify.php,"一切都将正常运行(即,告诉您是否输入了正确的reCaptcha,无论您尝试了多少次)。

Am I missing something? 我想念什么吗? Is there an easier, reliable way to do this? 有没有更简单,可靠的方法来做到这一点?

Use the onkeyup: false option inside .validate() . .validate()使用onkeyup: false选项。

Otherwise, every single time you type one letter, it will run your checkCaptcha method and send an incomplete Captcha code to the server. 否则,每次您键入一个字母,它将运行您的checkCaptcha方法并将不完整的验证码代码发送到服务器。 I believe once an incorrect Captcha code is sent, it's now expired and you can't send the same code again. 我相信一旦发送了错误的验证码,它现在已经过期,您将无法再次发送相同的代码。

Either way, I think it's best not to send a code to the server until after the field is complete. 无论哪种方式,我认为最好不要在该字段完成后才将代码发送到服务器。

$('#regForm').validate({
    onkeyup: false,
    rules:{
        recaptcha_response_field: {
            required: true,
            checkCaptcha: true
        }
    },
    messages:{
        recaptcha_response_field: {
            checkCaptcha: "Your Captcha response was incorrect. Please try again."
        }
    }
});

Also, if the Captcha fails, you'll need to trigger a refresh to get a new code. 另外,如果验证码失败,则需要触发刷新以获取新代码。

Recaptcha.reload();

Inside your success callback, perhaps... 在您的success回调中,也许...

success:function(resp) {
    if (resp == 'false') {
        console.dir(resp);
        Recaptcha.reload();  // get a new captcha code on failure
        return false;
    } else {
        console.dir(resp);
        return true;
    }
}

The problem with your first addMethod validator is about how you return true or false, and the second is more complex than what it should be. 第一个addMethod验证器的问题是关于如何返回true或false的,而第二个则要复杂得多。 I don't know how you treat the thing from server-side but maybe this code is what you need: 我不知道您如何从服务器端处理问题,但是也许这段代码是您所需要的:

http://blog.netgloo.com/2014/06/03/adding-recaptcha-validator-to-jquery-validate/ http://blog.netgloo.com/2014/06/03/adding-recaptcha-validator-to-jquery-validate/

That works great for me! 这对我来说很棒!

Alright, finally got it (although it just might be a work around). 好了,终于明白了(尽管可能只是一种解决方法)。 Here's the new addMethod : 这是新的addMethod

jQuery.validator.addMethod("checkCaptcha", function() {
 var bfr = false;
 var phpquery = $.ajax({url:"verify.php",
  type: "POST",
  async: false,
  data:{recaptcha_challenge_field:Recaptcha.get_challenge(),recaptcha_response_field:Recaptcha.get_response()},
  complete:function(resp) {
    if(resp.responseText.indexOf("false")>=0){
     Recaptcha.reload();
    }else{
     bfr = true;
    }
  }
 });
if(bfr){
   return true;
 }
},"");

I also added onkeyup:false , onclick:false , and onfocusout:false to validate() . 我还添加了onkeyup:falseonclick:falseonfocusout:falsevalidate() This seems to get the job done. 这似乎可以完成工作。

Here is my idea specially regarding ' validating captcha using AJAX request '. 这是我关于“ 使用AJAX请求验证验证码 ”的想法。

the main reason for adding a captcha in a form is for a verification whether it is a bot or human who did the form submission. 在表单中添加验证码的主要原因是为了验证提交表单的是机器人还是人类。 so even if other fields have errors, after a successful captcha submission obviously we know it is human. 因此,即使其他字段有错误,成功提交验证码后,我们显然也知道这是人为的。 So, It is totally fine to remove the captcha field after its first success (please do refresh the captcha until it is a success). 因此,在验证码字段首次成功之后将其删除是完全可以的(请刷新验证码,直到成功为止)。 Then can handle other fields as usualy. 然后可以照常处理其他字段。

Any objections ? 有异议吗?

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

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