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.
Latest posts by Sanju (see all)
- Top 10 Features in Angular 18 You Need to Know - June 25, 2024
- Specialities of Kerala’s agricultural and religious festival Vishu - November 25, 2022
- Why Kerala is known as God’s own country – Part 2 - June 20, 2019