Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google ReCAPTCHA how to make required?

Does anyone know how to make the "Google ReCAPTCHA (v2)" be "required" in a form?

I mean no form submission until recaptcha is filled-in?

I use ParsleyJs in my form, but didnt find a way to make it work with divs...

like image 777
serhio Avatar asked Apr 13 '15 18:04

serhio


1 Answers

You have to use the reCaptcha verify response call back. Something like this: <script src='https://www.google.com/recaptcha/api.js?onload=reCaptchaCallback&render=explicit'></script>

var RC2KEY = 'sitekey',
    doSubmit = false;

function reCaptchaVerify(response) {
    if (response === document.querySelector('.g-recaptcha-response').value) {
        doSubmit = true;
    }
}

function reCaptchaExpired () {
    /* do something when it expires */
}

function reCaptchaCallback () {
    /* this must be in the global scope for google to get access */
    grecaptcha.render('id', {
        'sitekey': RC2KEY,
        'callback': reCaptchaVerify,
        'expired-callback': reCaptchaExpired
    });
}

document.forms['form-name'].addEventListener('submit',function(e){
    if (doSubmit) {
        /* submit form or do something else */
    }
})
like image 177
colecmc Avatar answered Oct 13 '22 02:10

colecmc