How To Add Google reCAPTCHA Validation In JavaScript

Using Google reCAPTCHA , System Can Differentiate Between Bot & Real Users Which Enables Strong Security Against DDos Attack.


Why To Add Google reCaptcha In Website

As Google reCAPTCHA can differentiate between bots & real user , then due to this
  • We can block specific access to bots like form submission or action on clicking submit button
  • which saves our web hosting processing memory
  • which will result in fast website loading speed.

JavaScript Google reCAPTCHA

There are some reasons due to which one have to use JavaScript instead of php like unavailability of web server. If you don't have web server then you can't execute php codes.

Add Google reCAPTCHA in JavaScript

To add Google reCAPTCHA , visit https://www.google.com/recaptcha/admin/ and login with your google account. Then enter label , domain and captcha type and click on submit.


After that it will provide you site key & secret key. Copy it and save it anywhere.

Code For JavaScript Captcha


<script>
function doit()
{
var captchares= grecaptcha.getResponse();
if(captchares.length == 0)
{
	alert("Error");
 //or return false;
}
else
{
    alert("Success");
    //or return true;
}
}
</script>
<script type="text/javascript" src="https://www.google.com/recaptcha/api.js"></script>
<div class="g-recaptcha" data-sitekey="6LezEnwfAAAAANHGu2vEPu0YRBOAbBWwNJX0LXS4"></div> 
 <input type="submit" value="Submit" onClick="return doit();" >


In the above code , enter your site key in data-sitekey value.

Frequently Asked Questions

Can We Use Google reCAPTCHA On LocalHost

No , you cannot use reCAPTCHA on localhost as it require a domain during Google reCAPTCHA site creation.

Can We Use Other Site Key On Our Site

No , you have to use same site key for your site which is provided by Google reCAPTCHA during creation time.