要求用户单击google';s在提交表格之前的新回顾


Require User to click google's new recaptcha before form submission

我正在表单(HTML5)中使用谷歌的新repatcha:https://www.google.com/recaptcha

在提交表格之前,是否有方法根据需要检查并标记重述?我想在客户端而不是服务器端验证这一点。这样,我就不必回到表单并警告用户不要为captcha输入任何内容。

我可以使用任何javascript来检查用户是否在repatcha中输入了任何内容?

您可以检查id为g-recaptcha-response的文本区域字段。您可以执行以下操作:

$('form').submit(function(event) {
    if ( $('#g-recaptcha-response').val() === '' ) {
        event.preventDefault();
        alert('Please check the recaptcha');
    }
});

希望这对你有帮助。

使用Ankesh和Lammert的部分解决方案的普通JavaScript实现:

var form = document.getElementById('ctct-custom-form');
form.addEventListener("submit", function(event){
    if (grecaptcha.getResponse() === '') {                            
      event.preventDefault();
      alert('Please check the recaptcha');
    }
  }
, false);

表单提交侦听器代码的来源:如何在javascript中侦听表单提交事件?

grecaptcha.getResponse()-返回Recaptcha的响应。您可以在等条件下进行检查

grecaptcha.getResponse(opt_widget_id) === ""

可选的小部件ID,如果未指定,则默认为创建的第一个小部件。

参考编号:https://developers.google.com/recaptcha/docs/display

我试图改进rylanb的答案。下面的代码可以找到页面中激活g-captcha并阻止提交的所有表单。注意:它假设您的div.g-recaptchaform 的子级

const forms = document.querySelectorAll('div.g-recaptcha');
forms.forEach(form=> {
    const formParentElement = form.parentElement;
    formParentElement.addEventListener("submit", e => {
        if (grecaptcha.getResponse() === '') {
            e.preventDefault();
            alert('Please check the recaptcha');
        }
    }, false)
});

2022年的工作解决方案

就我个人而言,我无法使用我的captcha获得上述任何解决方案。所以我想我会为那些面临同样问题的人分享我目前的工作解决方案。

我在.js中的笔记应该能彻底解释解决方案。

JavaScript

// By default do not allow form submission.
var allow_submit = false
function captcha_filled () {
    /*
     * This is called when Google get's the recaptcha response and approves it.
     * Setting allow_submit = true will let the form POST as normal.
     * */
    allow_submit = true
}
function captcha_expired () {
    /*
     * This is called when Google determines too much time has passed and expires the approval.
     * Setting allow_submit = false will prevent the form from being submitted.
     * */
    allow_submit = false
}

function check_captcha_filled (e) {
    console.log('verify-captcha')
    /*
     * This will be called when the form is submitted.
     * If Google determines the captcha is OK, it will have
     * called captcha_filled which sets allow_submit = true
     * If the captcha has not been filled out, allow_submit
     * will still be false.
     * We check allow_submit and prevent the form from being submitted
     * if the value of allow_submit is false.
     * */
    // If captcha_filled has not been called, allow_submit will be false.
    // In this case, we want to prevent the form from being submitted.
    if (!allow_submit) {
        // This call prevents the form submission.
        // e.preventDefault()
        // This alert is temporary - you should replace it with whatever you want
        // to do if the captcha has not been filled out.
        alert('ERROR: Please verify you are human by filling out the captcha')
        return false
    }
    captcha_expired()
    return true
}

HTML

<form action="post" onsubmit="return check_captcha_filled()">
<!-- form items -->
<div class="g-recaptcha"
   data-callback="captcha_filled"
   data-expired-callback="captcha_expired"
   data-sitekey="your site key">
</div>
</form>

最简单的方法是更改按钮的类型。

    <button class="theme-btn" type="button" onclick="checkForm()" id="sign_up_button" >

现在创建一个Java脚本函数来更改验证时提交的类型。

<script type="text/javascript">

//无捕获功能

    function checkForm(){
      
        if(!document.getElementById("g-recaptcha-response").value){
            alert("Please Prove ! You're not a Robot");
            return false;
        }else{
            document.getElementById("sign_up_button").type = "submit";                
            return true;
        }
    }
 </script>