2

I have a form in my web but I've been receiving some span so I add Google RecaptchaV2 and it stop the span but I would like disable the Submit button until the Recaptcha is checked and the user prove that is human.

The RecaptchaV2 run inside a Iframe, how can I read an element or a property to disable the Submit?

Inside the Iframe there is a span that change content once is verified: from:

<span id="recaptcha-accessible-status">Recaptcha requires verification</span>

to:

<span id="recaptcha-accessible-status">You are verified</span>

So reading that I think might be possible disable/enable the submit button with Jquery, tried using content() to read it but can't access to the iframe content, any ideas?

Thank you!

ysanmiguel
  • 451
  • 1
  • 4
  • 20

2 Answers2

15

Here is the answer, this is just to disable/enable the submit button on check:

Made it to work with different forms in one page but not tested, any suggestions/changes are welcome.

Call:

<div class="g-recaptcha" data-sitekey="KEY-HERE" data-callback="correctCaptcha"></div>
<script type="text/javascript" src="https://www.google.com/recaptcha/api.js?hl=en"></script>

The data-callback="correctCaptcha" is the master piece.

js:

$("form").each(function() {
    $(this).find(':input[type="submit"]').prop('disabled', true);
});
function correctCaptcha() {
    $("form").each(function() {
        $(this).find(':input[type="submit"]').prop('disabled', false);
    });
}
ysanmiguel
  • 451
  • 1
  • 4
  • 20
  • great solution. and for vanilla JS I just gave my submit button an ID and used this line in the correctCaptcha function: document.getElementById("submitBtn").disabled = false; – LaZza Feb 01 '21 at 16:33
0

This is what I ended up doing:

  var verifyCallback = function(response) {
    $('.contactForm :input[type="submit"]').prop('disabled', false);
  };

  var onloadCallback = function() {        
    grecaptcha.render('contactForm', {
      'sitekey' : 'SITE-KEY-HERE',
      'callback' : verifyCallback,
      'theme' : 'light'
    });
  };
oucil
  • 4,211
  • 2
  • 37
  • 53
MikeBeaudin87
  • 115
  • 1
  • 6