To do so, you need some custom code, please follow below steps which work for me:
Note: I assume you used jQuery library on your frontend theme
Step-1: On document ready add some custom class to your reCAPTCHA
input field:
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery('#recaptcha_response_field').addClass('required-captcha-entry');
})
</script>
Step-2: Initialize Magento
VarienForm
javascript class object and add new validation rule for reCAPTCHA
field:
<script type="text/javascript">
var yourFormValidationObj = new VarienForm('your_form_id');
Validation.add('required-captcha-entry', ' ', function(v) {
return !Validation.get('IsEmpty').test(v);
})
</script>
Step-3: On form submit, validate form for required values
<script type="text/javascript">
if(yourFormValidationObj.validator.validate()){
// Form validate and ready to submit
jQuery('YOUR_FORM_IDENTIFIER').submit();
}
</script>
That's, your reCAPTCHA
field is become required field and validated before submit the form.
You should try this code: I have been using this on my site.
<script>
window.onload = function() {
var recaptcha = document.forms["contactForm"]["g-recaptcha-response"];
recaptcha.required = true;
recaptcha.oninvalid = function(e) {
alert("Please complete the captcha");
}
}
</script>
Best Answer
There are many extensions available online for Google Recaptcha. Checkout this extension - https://github.com/magespecialist/m2-MSP_ReCaptcha
You can take reference from this extension.