使用ReCaptcha和jQuery Validate,给出正确的响应,但会出错 [英] Using ReCaptcha with jQuery Validate, giving correct response but gives error
问题描述
我正在使用jQuery验证我的网站的注册表单,底部有一个reCaptcha。以下是检查表单是否有错误的脚本(仅编辑为相关部分):
I'm using jQuery validate with a registration form for my website with a reCaptcha at the bottom. 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."}
}
});
});
当我输入正确的reCaptcha响应并点击提交或制表符出响应文本时会发生什么字段,它最初会在控制台中说 true
,然后立即抛出 false
并阻止我提交表单。
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响应时,它会抛出 false
,就像它应该但是每一个我输入的字母,它会将其提交给 verify.php
并返回 false
。当我最终输入正确的reCaptcha时,它不能识别它并仍然返回 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
. When I finally get done typing in the correct reCaptcha, it doesn't recognize it as such and still returns false
.
即使输入了正确的响应,重新加载reCaptcha也会抛出 false
。
Reloading the reCaptcha also throws a false
, even when the correct response is entered.
我知道 verify.php
正常工作,因为当< form>
会有 action =verify.php,
一切都会完美运行(即告诉你是否输入了正确的reCaptcha,无论有多少你试过的次数)。
I know the verify.php
works correctly, because when the <form>
would have action="verify.php,"
everything would work perfectly (i.e., tell you if you entered the correct reCaptcha, regardless of how many times you tried).
我错过了什么吗?有没有更容易,更可靠的方法呢?
Am I missing something? Is there an easier, reliable way to do this?
推荐答案
好吧,终于明白了(虽然它可能是一个解决方法)。这是新的 addMethod
:
Alright, finally got it (although it just might be a work around). Here's the new 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;
}
},"");
我还添加了 onkeyup:false
, onclick:false
, onfocusout:false
到 validate()
。
这似乎可以完成工作。
I also added onkeyup:false
, onclick:false
, and onfocusout:false
to validate()
.
This seems to get the job done.
这篇关于使用ReCaptcha和jQuery Validate,给出正确的响应,但会出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!