javascript 检查 google recaptcha 是否已检查或已填写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32218183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
check if google recaptcha is checked or has been filled
提问by Juliver Galleto
im using google recaptcha
我正在使用谷歌验证码
<label for="template-contactform-botcheck">Botcheck</label>
<div id="recaptcha_sme"></div>
and I want to verify if its checked or not, has been filled or not and the way im doing it is through this code (refer below)
我想验证它是否已检查,是否已填充以及我这样做的方式是通过此代码(请参阅下文)
if ($("#recaptcha_sme #recaptcha-anchor").hasClass("recaptcha-checkbox-checked")) {
alert("the captcha has been filled");
} else {
alert("Please fill the captcha!");
}
and I don't intend to go trough server side validation (google recaptcha API checking), I just want to be notified (using alert prompt) if the google recaptcha is checked or filled. Any ideas, clues, suggestions, recommendations to make it?
并且我不打算通过服务器端验证(google recaptcha API 检查),我只想在检查或填充 google recaptcha 时收到通知(使用警报提示)。有什么想法、线索、建议、建议来制作它吗?
below is my complete form submit script code (submit function in jquery).
下面是我完整的表单提交脚本代码(jquery 中的提交函数)。
$("#sme_application_dialog form").submit(function(e){
if ($(this).find("#recaptcha_sme #recaptcha-anchor").hasClass("recaptcha-checkbox-checked")) {
alert("the captcha has been filled");
} else {
alert("Please fill the captcha!");
}
e.preventDefault();
});
as you can see from above reference, I'm just trying to check if the element that has an id of "recaptcha-anchor" has a class of "recaptcha-checkbox-checked" (as i can see from DOM structure using chrome dom explorer, that class has added unto the element that has an id of "recaptcha-anchor everytime the google recaptcha is checked or has been filled) so I thought that might work but sadly and unfortunately it doesn't work.
正如您从上面的参考资料中看到的,我只是想检查 ID 为“recaptcha-anchor”的元素是否具有“recaptcha-checkbox-checked”类(正如我从使用 chrome dom 的 DOM 结构中看到的那样资源管理器,该类已添加到 ID 为“recaptcha-anchor 每次检查或填充谷歌 recaptcha 的元素”,所以我认为这可能会起作用,但遗憾的是它不起作用。
PS: assume that I have div that has an id of "sme_application_dialog_form" and inside it there is a from and inside that form, there is the google recaptcha and I have "$(document).ready". Assume that everything set and working (except for the validation where I'm checking the google recaptcha is checked or has been filled)
PS:假设我有一个 id 为“sme_application_dialog_form”的 div,里面有一个 from 和里面的表单,有谷歌 recaptcha,我有“$(document).ready”。假设一切都已设置并正常工作(除了我正在检查谷歌 recaptcha 的验证是否已检查或已填写)
采纳答案by Vigneshwar
You can validate it on callback function by getting response value is null or not.
您可以通过获取响应值是否为空来在回调函数上验证它。
callback function
回调函数
function submit(){
var response = grecaptcha`.`getResponse();
if(response != '0'){
//captcha validated and got response code
alert("the captcha has been filled");
}else{
//not validated or not clicked
alert("Please fill the captcha!");
}
}
It will work.
它会起作用。
回答by Shahar Shokrani
Add data-callback
to your ReCapthca
div:
添加data-callback
到您的ReCapthca
div:
<div class="g-recaptcha" data-sitekey="***" data-callback="recaptchaCallback"></div>
Add this function to your code.
将此函数添加到您的代码中。
var recaptchachecked;
function recaptchaCallback() {
//If we managed to get into this function it means that the user checked the checkbox.
recaptchachecked = true;
}
You may now can use the recaptchachecked on your OnSubmit() function.
您现在可以在 OnSubmit() 函数上使用 recaptchachecked。