3

I have got more than 4 google recaptcha on the same page. I managed to get them showing, but validating is not working.

$('#newsletter').on('submit', function(e) { 
      var response = grecaptcha.getResponse();
      if (response.length == 0) {
          e.preventDefault();
          alert("Incorrect captcha code. Please try again.");
          return false;
      } else {
          return true;
      } 
});
<form action="" method="post" id="newsletter">
      <div id="sideRecaptcha" class="g-recaptcha"></div>
      <input type="submit" value="submit" />
</form>
 <script src="https://www.google.com/recaptcha/api.js?onload=reCaptchaCallback&render=explicit&hl=en" async defer></script>
 <script>
      var reCaptchaCallback = function() {
   var elements = document.getElementsByClassName('g-recaptcha');
   for (var i = 0; i < elements.length; i++) {
        var id = elements[i].getAttribute('id');
        var widgetId = grecaptcha.render(id, {
        'sitekey' : 'XXXXXXXX',
        theme' : 'light'
   });
        elements[i].setAttribute('cid', widgetId);
        grecaptcha.reset(widgetId);
   }
};
</script>

grecaptcha.getResponse() always return empty. Any idea?

Alex Chen
  • 133
  • 1
  • 10
  • Possible duplicate of [How do I show multiple recaptchas on a single page?](http://stackoverflow.com/questions/1241947/how-do-i-show-multiple-recaptchas-on-a-single-page) – tanaydin May 22 '17 at 02:24
  • I have no problem adding multiple recaptchas on a page. problem is validating not working on submit. – Alex Chen May 22 '17 at 02:34
  • there is answer for your question, please read it before committing something. – tanaydin May 22 '17 at 13:49
  • @tanaydin The problem is about validation of multiple recaptcha not working on submit. It is not a duplicate and seems like there is no answer to this. AlexChen did you find a way? I have the same problem. – Sara Briccoli Sep 01 '22 at 13:00

0 Answers0