Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 949
Hello Universe Avatar asked Feb 23 '15 13:02

Hello Universe


People also ask

How do I validate a Google reCAPTCHA on a form?

The Best Answer is. If you want to check if the User clicked on the I'm not a robot checkbox, you can use the . getResponse() function provided by the reCaptcha API. In case the User has validated himself, the response will be a very long string.

How do I validate an invisible reCAPTCHA?

Invoking the reCAPTCHA verification programmatically can be achieved by rendering the challenge in a div with an attribute data-size="invisible" and programmatically calling execute. Create a div with data-size="invisible" . Call grecaptcha. execute from a javascript method.


Video Answer


2 Answers

Google has 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');
};
like image 114
slinky2000 Avatar answered Oct 16 '22 08:10

slinky2000


You can also call the grecaptcha object to check. grecaptcha.getResponse(); is empty when unchecked and has the verification code when checked.

grecaptcha.getResponse().length === 0 when unchecked

function isCaptchaChecked() {
  return grecaptcha && grecaptcha.getResponse().length !== 0;
}

if (isCaptchaChecked()) {
  // ...
}
like image 81
Olafur Tryggvason Avatar answered Oct 16 '22 10:10

Olafur Tryggvason