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

前端 未结 11 1620
既然无缘
既然无缘 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:52

    Here's some code to accompany @tzafar's answer:

    var myCaptcha = null;
    function prepCaptcha() {
        if (myCaptcha === null)
            myCaptcha = grecaptcha.render(document.getElementById('my-captcha'), {
                'sitekey': myCaptchaKey,
                'theme': 'light'
            });
        else
            grecaptcha.reset(myCaptcha);
    }
    

    In my case, my-captcha is the id of a div inside of a Bootstrap modal. I run prepCaptcha() in the event handler for the modal's shown.bs.modal event. The first time, it initializes the captcha. On subsequent shows of the modal, it resets it.

    Also, note that you can quickly verify that you're getting a new captcha by printing the completed captcha's value:

    console.log(grecaptcha.getResponse(myCaptcha));
    

    You shouldn't see the same value twice.

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

    if you are using new recaptcha 2.0 use this: for code behind:

    ScriptManager.RegisterStartupScript(this, this.GetType(), "CaptchaReload", "$.getScript(\"https://www.google.com/recaptcha/api.js\", function () {});", true);
    

    for simple javascript

    <script>$.getScript(\"https://www.google.com/recaptcha/api.js\", function () {});</script>
    
    0 讨论(0)
  • 2020-12-25 12:52

    If you're using WordPress, make sure you don't have another reCaptcha plugin installed...that was the issue for me.

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

    your recaptcha render js event should be called only once in the script, your recaptcha code is not valid if grecaptcha.render js event called second time in script. You should have to check your code so it should not call the grecaptcha.render function second time on validation check.

    OR

    In console, You will see a Uncaught ReferenceError: Recaptcha is not defined error message, which indicates the problem with the reCAPTCHA script. This is caused due to a bad url in the page - http://api.recaptcha.net/js/recaptcha_ajax.js . Google has updated the reCAPTCHA and moved the location of the script to http://www.google.com/recaptcha/api/js/recaptcha_ajax.js .

    also check this post

    http://www.instructables.com/id/Fix-reCAPTCHA-errors-on-Instructables/

    and this post

    Uncaught ReferenceError: Recaptcha is not defined

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

    Personnaly I reset my element html content with

    $('#captcha').html('');
    

    after that I reload recaptcha using

    $.getScript("https://www.google.com/recaptcha/api.js");
    

    which load recaptcha content on your

    <div id="captcha" class="g-recaptcha" data-sitekey="yourSitePublicKey"></div>
    
    0 讨论(0)
提交回复
热议问题