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)
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.
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.
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.
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.
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