How to validate google reCAPTCHA v2 using JavaScript?

Sometimes, we want to validate google reCAPTCHA v2 using JavaScript.

In this article, we’ll look at how to validate google reCAPTCHA v2 using JavaScript.

How to validate google reCAPTCHA v2 using JavaScript?

To validate google reCAPTCHA v2 using JavaScript, we use the grecaptcha.getRespons method.

For instance, we write

const response = grecaptcha.getResponse();

if (response) {
  // ...
} else {
  //...
}

to call grecaptcha.getResponse method to get the captcha validation result.

If response isn’t null, then the captcha is verified.

Otherwise, it’s not.

Conclusion

To validate google reCAPTCHA v2 using JavaScript, we use the grecaptcha.getRespons method.