Skip to content

Instantly share code, notes, and snippets.

@dillansimmons
Created October 21, 2016 05:51
Show Gist options
  • Save dillansimmons/17fb66a8956fc2610ab87bd7e46e92d6 to your computer and use it in GitHub Desktop.
Save dillansimmons/17fb66a8956fc2610ab87bd7e46e92d6 to your computer and use it in GitHub Desktop.
Insert Google reCAPTCHA into Marketo form with validation
/* Load the Google reCAPTCHA API : Make sure it loads somewhere above the insert code-->
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
*/
/* Main code that inserts the reCAPTCHA into the marketo form */
// Wait for Marketo form to load
MktoForms2.whenReady(function (form) {
// Insert the recap div after the last form row
$( '<div id="recap"></div>' ).insertAfter( ".mktoFormRow:last-of-type" );
// render the recap : replace with your site key
grecaptcha.render('recap', {
'sitekey' : 'INSERT_YOUR_SITE_KEY',
});
// Validate form based on Recap
document.getElementById("mktFrmSubmit").onclick = function(){
var v = grecaptcha.getResponse();
if(v.length == 0) {
event.preventDefault();
form.submittable(false);
// You can put some css error response here if you want
}
else {
form.submittable(true);
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment