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.
Just replace ". form_div" with whatever div houses your form. Now when the user hits the back button it refreshes the recaptcha image.
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.
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.
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>
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' });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With