Here's an example that starts with the submit button that is disabled and turns it on after receiving a callback from reCaptcha. It also uses jquery validation to make sure the form is valid before submitting.
var UserSubmitted = { $form: null, recaptcha: null, init: function () { this.$form = $("#form").submit(this.onSubmit); }, onSubmit: function (e) { if ($(this).valid()) { var response = grecaptcha.getResponse(); if (!response) { e.preventDefault(); alert("Please verify that you're a human!"); } } }, setupRecaptcha: function (key) { UserSubmitted.recaptcha = grecaptcha.render('recaptcha', { 'sitekey': key, 'callback': UserSubmitted.verifyCallback
I call setupRecaptcha from a page with a named function that the js part includes.
<script> var recaptchaLoader = function () { UserSubmitted.setupRecaptcha("yourkey"); }; </script> <script src="https://www.google.com/recaptcha/api.js?onload=recaptchaLoader&render=explicit" async defer></script>
You can simplify this. I use it in a multi-user application with different keys, and UserSubmitted is actually part of a larger library. You cannot use namespaces (UserSubmitted.somefunction) as an onload parameter (as far as I know).
source share