How to reload the google recaptcha widget after user submits invalid inputs

前端 未结 11 1619
既然无缘
既然无缘 2020-12-25 12:08

I have a registration form with the new google recaptcha widget on it. When a user submits info, we check the validation with javascript and return the problems to the page

相关标签:
11条回答
  • 2020-12-25 12:33
    <script>
    function cform(token) {
    $.ajax({
    type: 'POST',
    url: 'contact_chk.php',
    data: $('#cform').serialize(), 
    success: function(response) {
    grecaptcha.reset();
    $("#con_msg").html(response);
    document.getElementById("name").value='';
    document.getElementById("subject").value='';
    document.getElementById("email").value='';
    document.getElementById("phone").value='';
    document.getElementById("message").value='';
    },
    error: function(response) {
    grecaptcha.reset();
    $("#con_msg").html('Try Again...');
    }
    });
    }
    </script>
    <script src='https://www.google.com/recaptcha/api.js'></script>
    
    0 讨论(0)
  • 2020-12-25 12:35

    I had same problem with excelwebzone/recaptcha-bundle for Symfony2. Basicly is says that recaptcha was loaded in past to your DOM and placeholder that should be empty was full.

    Those bundles dont have settings for explicit callbacks so you have to make sure that placeholder or even whole form is empty before you load recaptcha.

    And mine was stuck in DOM cause i was loading it with AJAX. On second call it was there.

    You can simply use $('#your-div-with-form').html('')

    0 讨论(0)
  • 2020-12-25 12:41

    If you are using grecaptcha.render with jQuery, please ensure that you are actualy setting DOM element, instead of jQuery element:

    This will work:

    grecaptcha.render( $('.g-recaptcha')[0], { sitekey : 'your_key_here' });
    

    but this wont:

    grecaptcha.render( $('.g-recaptcha'), { sitekey : 'your_key_here' });
    
    0 讨论(0)
  • 2020-12-25 12:45

    The method you are looking for is grecaptcha.reset()

    However, in order for this function to work you have to render the reCaptacha explicitly like this : <script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit" async defer></script>

    0 讨论(0)
  • 2020-12-25 12:49

    Try calling the reload() function within ReCaptcha (version 1):

    Recaptcha.reload();
    

    How to Reload ReCaptcha using JavaScript?

    0 讨论(0)
  • 2020-12-25 12:51

    I was able to do it just by using:

    grecaptcha.reset();
    
    0 讨论(0)
提交回复
热议问题