Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if google recaptcha is checked or has been filled

im using google recaptcha

<label for="template-contactform-botcheck">Botcheck</label>
<div id="recaptcha_sme"></div>

and I want to verify if its checked or not, has been filled or not and the way im doing it is through this code (refer below)

if ($("#recaptcha_sme #recaptcha-anchor").hasClass("recaptcha-checkbox-checked")) {
    alert("the captcha has been filled");
} else {
    alert("Please fill the captcha!");
}

and I don't intend to go trough server side validation (google recaptcha API checking), I just want to be notified (using alert prompt) if the google recaptcha is checked or filled. Any ideas, clues, suggestions, recommendations to make it?

below is my complete form submit script code (submit function in jquery).

$("#sme_application_dialog form").submit(function(e){
    if ($(this).find("#recaptcha_sme #recaptcha-anchor").hasClass("recaptcha-checkbox-checked")) {
        alert("the captcha has been filled");
    } else {
        alert("Please fill the captcha!");
    }
    e.preventDefault();


});

as you can see from above reference, I'm just trying to check if the element that has an id of "recaptcha-anchor" has a class of "recaptcha-checkbox-checked" (as i can see from DOM structure using chrome dom explorer, that class has added unto the element that has an id of "recaptcha-anchor everytime the google recaptcha is checked or has been filled) so I thought that might work but sadly and unfortunately it doesn't work.

PS: assume that I have div that has an id of "sme_application_dialog_form" and inside it there is a from and inside that form, there is the google recaptcha and I have "$(document).ready". Assume that everything set and working (except for the validation where I'm checking the google recaptcha is checked or has been filled)

like image 402
Juliver Galleto Avatar asked Aug 26 '15 04:08

Juliver Galleto


People also ask

How do I check my reCAPTCHA response?

For web users, you can get the user's response token in one of three ways: g-recaptcha-response POST parameter when the user submits the form on your site. grecaptcha. getResponse(opt_widget_id) after the user completes the reCAPTCHA challenge.

How do I know if my Google reCAPTCHA key is valid?

Google does not provide methods to verify site key, neither you can hack/access reCaptcha html code by JS since the reCapthca is in an iframe and frame's code is not programmatically accessible client-side. Its a common mistake to forget to update the allowable domains in the dashboard when pushing a new site live.

How do I know if CAPTCHA v3 is working?

If reCAPTCHA v3 is working correctly on the front-end of your site a small reCAPTCHA badge should appear at the bottom right of each page. Likewise, since it is no longer necessary in reCAPTCHA v3, a form-tag generator button for the reCAPTCHA widget ( [recaptcha] ) does not appear in the Form tab panel.


1 Answers

Add data-callback to your ReCapthca div:

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

Add this function to your code.

var recaptchachecked;
function recaptchaCallback() {
    //If we managed to get into this function it means that the user checked the checkbox.
    recaptchachecked = true;
}

You may now can use the recaptchachecked on your OnSubmit() function.

like image 168
Shahar Shokrani Avatar answered Oct 06 '22 00:10

Shahar Shokrani