{"id":10100,"date":"2021-06-14T11:00:28","date_gmt":"2021-06-14T02:00:28","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=10100"},"modified":"2021-06-11T21:51:07","modified_gmt":"2021-06-11T12:51:07","slug":"adding-recaptcha-verification-to-your-form","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/10100","title":{"rendered":"Adding Recaptcha verification to your form"},"content":{"rendered":"\n<p>We all create forms in your system &amp; applications. If you are a web developer , you may experience once or more creating forms. For example contact form , registration form etc. Recaptcha is a free service supported by Google which helps to protect our websites from spam and abuse. Basically A \u201c<em>CAPTCHA<\/em>\u201d is a test to separate human and bots apart.<\/p>\n\n\n\n<p>Today I will guide you how to add a reCAPTCHA service in your form easily.<\/p>\n\n\n\n<p>First thing first, we need to generate a reCAPTCHA key to add in our form. You can go to this <a href=\"https:\/\/www.google.com\/recaptcha\/admin\/create\">link<\/a> and click + button to create a key. <\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"1024\" height=\"577\" src=\"\/appnews\/wp-content\/uploads\/sites\/4\/2021\/06\/image-5-1024x577.png\" alt=\"\" class=\"wp-image-10102\" srcset=\"https:\/\/www.gigas-jp.com\/appnews\/wp-content\/uploads\/sites\/4\/2021\/06\/image-5-1024x577.png 1024w, https:\/\/www.gigas-jp.com\/appnews\/wp-content\/uploads\/sites\/4\/2021\/06\/image-5.png 1814w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>This will proceed to a form to choose the options like this.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"1024\" height=\"818\" src=\"\/appnews\/wp-content\/uploads\/sites\/4\/2021\/06\/image-6-1024x818.png\" alt=\"\" class=\"wp-image-10103\"\/><\/figure>\n\n\n\n<p>There are two versions of reCAPTCHA type for now , version 2 and 3. With reCAPTCHA v2, the only action required was to check if the user successfully solved the challenge or not. With reCAPTCHA v3, you must now decide what action to take based on the score. you may choose depending on your favour. In this tutorial , I will use v2 as I like its simplicity.<\/p>\n\n\n\n<p>After filling up the form, you will get the reCAPTCHA site key like this. <\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"1024\" height=\"432\" src=\"\/appnews\/wp-content\/uploads\/sites\/4\/2021\/06\/image-7-1024x432.png\" alt=\"\" class=\"wp-image-10104\" srcset=\"https:\/\/www.gigas-jp.com\/appnews\/wp-content\/uploads\/sites\/4\/2021\/06\/image-7-1024x432.png 1024w, https:\/\/www.gigas-jp.com\/appnews\/wp-content\/uploads\/sites\/4\/2021\/06\/image-7.png 1904w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption><br><\/figcaption><\/figure>\n\n\n\n<p>For the source code part, we need to import the <a href=\"https:\/\/www.google.com\/recaptcha\/api.js\">reCaptcha API JS <\/a> in a script tag like this.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;script src=https:\/\/www.google.com\/recaptcha\/api.js>&lt;\/script><\/code><\/pre>\n\n\n\n<p>And we can embed our site key in HTML.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div class=\"g-recaptcha\" data-sitekey=\"YOUR-SITE-KEY-HERE\">&lt;\/div><\/code><\/pre>\n\n\n\n<p>Of course you can modify the css class as you like. For the validation, we can use <code>grecaptcha.getResponse()<\/code>function to validate the reCAPTCHA response. I will add a example code which is used in my project.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/This is just a sample code and can differ depending on your \/\/source code.\nif ($('.g-recaptcha').length > 0) {\n   if (grecaptcha.getResponse() == \"\") {\n      flag = 1;\n        alert('Please verify Recaptch');\n   } else {\n      flag = 0;\n   }\n}\n\n\/\/ You can also read the official documentation.\n\/\/ https:\/\/developers.google.com\/recaptcha\/intro\n<\/code><\/pre>\n\n\n\n<p>As a final result on the browser, you will get a reCAPTCHA checkbox like this. This may also differ depending on your design preferences.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"1024\" height=\"229\" src=\"\/appnews\/wp-content\/uploads\/sites\/4\/2021\/06\/image-8-1024x229.png\" alt=\"\" class=\"wp-image-10105\" srcset=\"https:\/\/www.gigas-jp.com\/appnews\/wp-content\/uploads\/sites\/4\/2021\/06\/image-8-1024x229.png 1024w, https:\/\/www.gigas-jp.com\/appnews\/wp-content\/uploads\/sites\/4\/2021\/06\/image-8.png 1308w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>By Yuuma<\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/10100\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/10100\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"Adding Recaptcha verification to your form\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/10100\" data-text=\"Adding Recaptcha verification to your form\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/10100\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/10100\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>We all create forms in your system &amp; applications. If you are a web developer , you may experience once or [&hellip;]<\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[35],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/10100"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=10100"}],"version-history":[{"count":3,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/10100\/revisions"}],"predecessor-version":[{"id":10111,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/10100\/revisions\/10111"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=10100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=10100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=10100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}