简体   繁体   English

检查是否已选中google recaptcha或已将其填满

[英]check if google recaptcha is checked or has been filled

im using google recaptcha 我正在使用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. 而且我不打算通过服务器端验证(检查Google Recaptcha API),仅想在检查或填写Google Recaptcha时得到通知(使用警报提示)。 Any ideas, clues, suggestions, recommendations to make it? 有什么想法,线索,建议,建议吗?

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为“每次检查或重新填充Google Recaptcha时都具有recaptcha-anchor”的元素,因此我认为这可能有效,但不幸的是,它不起作用。

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". PS:假设我有一个id为“ sme_application_dialog_form”的div,并且在该表单中有一个from和inside形式,有google recaptcha,我有“ $(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) 假设一切都设置并正常工作(除了我正在检查google recaptcha的验证是否已检查或已填充)

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。

You can validate it on callback function by getting response value is null or not. 您可以通过获取响应值是否为null来在回调函数上对其进行验证。

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. 它会工作。

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

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