29 May

Google reCaptcha javascript validation

google recaptcha 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
Follow me

About Sanju

I’m Sanjunath.S from Mumbai, India. I like web technologies, traveling & history. I have 11+ years of experience in web technologies like HTML 5, CSS 3, JavaScript, Bootstrap, and PHP.




Pay with PayPal
Translate »