Category Archives: javascript

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.

Translate »