I’m having some issues on implementing Google recaptcha validation on a single page having multiple forms.
Reading many posts on the argument has led me to this design, that includes the possibility of using html5 validations before submitting the recaptcha validAtion:
HTML side
<head>
....
<script src="https://www.google.com/recaptcha/enterprise.js?onload=CaptchaCallback&render=explicit" async defer></script>
</head>
<body>
...
<form id="contactFrm1">
...
<div id="RecaptchaField1" class="g-recaptcha" data-callback="formSubmit1"></div>
...
</form>
...
...
<form id="contactFrm2">
...
<div id="RecaptchaField2" class="g-recaptcha" data-callback="formSubmit2"></div>
...
</form>
..
</body>
Javascript Side
<script>
var CaptchaCallback = function() {
jQuery('.g-recaptcha').each(function(index, el) {
var widgetId = grecaptcha.enterprise.render(el, {'sitekey' : SITE_KEY});
jQuery(this).attr('data-widget-id', widgetId);
});
};
$(document).ready(function() {
$("#submit").click(function(){
$('#contactFrm1').submit(function(event) {
if (!grecaptcha.enterprise.getResponse(jQuery('#RecaptchaField1').attr('data-widget-id'))){
event.preventDefault(); //prevent form submit
grecaptcha.enterprise.execute(jQuery('#RecaptchaField1').attr('data-widget-id'));
}
});
formSubmit = function() {
$('#contactFrm1').submit();
}
});
});
//same logic for the second form
</script>
On rendering the page shows, for both of captcha widgets, “Note for site owner: Invalid key type”.
I’m sure about the validity of the key, due to the fact that using the “not explicit” rendering do not return any kind of error and, at last, the key is valid from the Google administration settings.
Can you please help me to understand what is wrong with this solution?
Thanks in advance
Some posts showed that the problem should be on the execute methods, that it’s expecting the widget_id, in a first version I didn’t provide it, but after introducing it the result hasn’t changed.
Google documentation is not really exhaustive…