Hello Universe Hello Universe - 6 months ago 154
Javascript Question

How to check in js that user has checked the checkbox in Google recaptcha?

I have added the following before end of head

<script src='https://www.google.com/recaptcha/api.js'></script>

I have added this before end of form

<div class="g-recaptcha" data-sitekey="== xxxxxx =="></div>

I can see the recaptcha similar to https://developers.google.com/recaptcha/

HOwever, when user presses data without checking the checkbox, the data is submitted. Is there any other code I need to add to check if user has pressed the checkbox? Hopefully in js?


Google have a call back option for when the checkbox is checked.

Add this to your form element:



<div class="g-recaptcha" data-callback="recaptchaCallback" data-sitekey="== xxxxxx =="></div>

And a disable attribute to your submit button.


<button id="submitBtn" disabled>Submit</button>

Then a create a callback function and write whatever code you need.


function recaptchaCallback() {