Hello Universe Hello Universe - 5 months ago 113
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?

Answer

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

Add this to your form element:

data-callback="XXX"

Example:

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

And a disable attribute to your submit button.

Example:

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

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

Example:

function recaptchaCallback() {
    $('#submitBtn').removeAttr('disabled');
};
Comments