29 May
Google reCaptcha javascript validation

This is a simple solution for Google recaptcha client side javascript validation.
This way you can handle the validation of the response in your form submit handler using javascript. This seems more simple solution than trying to handle it on the captcha callback.
<script type="text/javascript" language="javascript"> function validateform(){ var captcha_response = grecaptcha.getResponse(); if(captcha_response.length == 0) { // Captcha is not Passed return false; } else { // Captcha is Passed return true; } } // ]]></script>
<script type="text/javascript" src="https://www.google.com/recaptcha/api.js"></script></pre> <form action="" method="post" onsubmit="return validateform();"> <div class="g-recaptcha" data-sitekey="your_sitekey"></div> <input type="submit" value="Submit" /></form> <pre>
“grecaptcha.getResponse()” will return a null value, if recaptcha validation is not passed on client side. Else it will return a value. So we need to check the length of the response (null or not), for recaptcha is valid or not valid.
Sanju
I’m Sanjunath.S from Mumbai, India. I like web technologies, traveling & history. I have 9+ years experience in web technologies like HTML 5, CSS 3, JavaScript, Bootstrap and PHP.
Latest posts by Sanju (see all)
- Why Kerala is known as God’s own country - November 1, 2018
- Basic tips – Mobile application development with Ionic - October 15, 2018
- Angular [ innerHTML ] – Elements id attribute is missing in innerHTML - September 23, 2018
Please follow and like us:
More from w3blog
![]() |