【问题标题】:check if google recaptcha is checked or has been filled检查 google recaptcha 是否已检查或已填写
【发布时间】:2015-11-19 23:52:21
【问题描述】:

我正在使用谷歌recaptcha

<label for="template-contactform-botcheck">Botcheck</label>
<div id="recaptcha_sme"></div>

我想验证它是否被选中,是否已被填充,我这样做的方式是通过这段代码(参见下文)

if ($("#recaptcha_sme #recaptcha-anchor").hasClass("recaptcha-checkbox-checked")) {
    alert("the captcha has been filled");
} else {
    alert("Please fill the captcha!");
}

并且我不打算通过服务器端验证(google recaptcha API 检查),我只想在检查或填写 google recaptcha 时收到通知(使用警报提示)。有什么想法、线索、建议、建议吗?

下面是我完整的表单提交脚本代码(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();


});

正如您从上面的参考资料中看到的那样,我只是想检查 id 为“recaptcha-anchor”的元素是否具有“recaptcha-checkbox-checked”类(从 DOM 结构中可以看出)使用chrome dom explorer,该类已添加到ID为“每次检查或填充google recaptcha时recaptcha-anchor的元素中)所以我认为这可能有效,但遗憾的是它不起作用。

PS:假设我有一个 id 为“sme_application_dialog_form”的 div,并且在其中有一个 from 和该表单内部,有 google recaptcha,我有“$(document).ready”。假设一切都已设置并正常工作(除了我正在检查 google recaptcha 的验证已检查或已填写)

【问题讨论】:

标签: javascript jquery recaptcha


【解决方案1】:

data-callback 添加到您的ReCapthca div:

<div class="g-recaptcha" data-sitekey="***" data-callback="recaptchaCallback"></div>

将此函数添加到您的代码中。

var recaptchachecked;
function recaptchaCallback() {
    //If we managed to get into this function it means that the user checked the checkbox.
    recaptchachecked = true;
}

您现在可以在 OnSubmit() 函数上使用 recaptchachecked。

【讨论】:

【解决方案2】:

您可以通过获取响应值是否为空来在回调函数上验证它。

回调函数

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!");
     }
}

它会起作用的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-29
    • 1970-01-01
    • 2019-07-23
    • 1970-01-01
    相关资源
    最近更新 更多