Skip to content

Instantly share code, notes, and snippets.

@man4toman
Last active March 13, 2022 23:42
Show Gist options
  • Save man4toman/0940d69160f5479f4c4a8d0dcf2c2ad4 to your computer and use it in GitHub Desktop.
Save man4toman/0940d69160f5479f4c4a8d0dcf2c2ad4 to your computer and use it in GitHub Desktop.
Google recaptcha v3 - JavaScript
<form method="post">
<div class="g-recaptcha" data-sitekey="SITE_KEY" data-callback="verifyCaptcha"></div>
<div id="g-recaptcha-error"></div>
<input type="button" class="submit" value="Submit" />
</form>
<script src='https://www.google.com/recaptcha/api.js'></script>
<script>
var recaptcha_response = '';
jQuery('.submit').on('click', function(){
if(recaptcha_response.length == 0) {
document.getElementById('g-recaptcha-error').innerHTML = '<span style="color:red;">This field is required.</span>';
return false;
}
return true;
})
function verifyCaptcha(token) {
recaptcha_response = token;
document.getElementById('g-recaptcha-error').innerHTML = '';
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment