Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 (ex: "please use a valid phone number"). however, since the page doesn't reload, when the user enters correct info and goes to submit again (without having to do the recaptcha again)...the recaptcha is no longer valid and they can't submit without reloading the page.

is there a good solution to this? can I reload the widget somehow? i tried grecaptcha.render but it didn't really do anything.

EDIT:

When I try to render the widget again, it says "placeholder element must be empty"

I tried emptying the element which seemed to work ($('.g-recaptcha').empty();) and then rendering but it still threw the same error.

like image 373
Matthew Berman Avatar asked Dec 19 '14 22:12

Matthew Berman


People also ask

How do I refresh Google reCAPTCHA?

Just replace ". form_div" with whatever div houses your form. Now when the user hits the back button it refreshes the recaptcha image.

How long does it take for reCAPTCHA to reset?

Note: reCAPTCHA tokens expire after two minutes. If you're protecting an action with reCAPTCHA, make sure to call execute when the user takes the action rather than on page load. You can execute reCAPTCHA on as many actions as you want on the same page.

Why reCAPTCHA is not showing sometimes?

One of the most common reasons why this error occurs is that of an outdated Chrome version. reCAPTCHA will actively look at the browser version before allowing you access. This is applicable to all browser versions, not just Chrome. In this case, the solution is to update Google Chrome to the latest version.


2 Answers

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>

like image 64
Ghislaindj Avatar answered Sep 28 '22 03:09

Ghislaindj


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' }); 
like image 20
Déján Kőŕdić Avatar answered Sep 28 '22 05:09

Déján Kőŕdić