1

I want to add google captcha on contact us page.

copied

vendor/magento/module-contact/view/frontend/templates/form.phtml

to

app/design/frontend/Vendor/MyTheme/Magento_Contact/templates/form.phtml

is there any simple widget to call google captcha on form.phtml?

asked May 2, 2019 at 11:13
4
  • What is your magento version? Commented May 2, 2019 at 11:20
  • I am using 2.2.5 Commented May 2, 2019 at 11:25
  • 1
    mageknowledge.com/validate-google-captcha-via-javascript Commented May 2, 2019 at 11:29
  • @Ghulam.M thanks for the link. I think that would work. Please add this in answer so that I can accept it after trying. Commented May 2, 2019 at 11:31

2 Answers 2

5

Suppose You have Form Similar to the code below

 <form id="myformId" class="col s12" action=""
 method="post"
 novalidate>
 <input placeholder="Name" id="mpfaqs-form-fieldset-input" type="text" name="username">
 <input placeholder="Email" id="mpfaqs-form-fieldset-input" type="text" name="useremail">
 <input placeholder="What is your question" id="mpfaqs-form-fieldset-input-question"
 type="text" name="userquestion">
 <div class="g-recaptcha" data-sitekey="YOURSITEKEYxxxxx"></div>
 <input class="form-submit" type="Submit" name="" value="Submit a question">
</form>
<script src='https://www.google.com/recaptcha/api.js'></script>
<script src='https://www.google.com/recaptcha/api.js?hl=es'></script>

To Validate above Form through javascript we will use the below code you can place it in the same file or create an external file for it and place the code

 <script>
 requirejs([
 'jquery'
 ], function($) {
 $(document).on('submit', '#myformId', function(){
 var isSubmit = jQuery(this).valid() && grecaptcha.getResponse() != '';
 if (!isSubmit) {
 alert("Please fill captcha");
 }
 return isSubmit;
 });
 })
</script>
answered May 2, 2019 at 11:33
3
  • Thanks for the answer. +1 and accepted :) Commented May 2, 2019 at 11:45
  • Excellent and working very fine. +1 Commented Jun 1, 2020 at 21:38
  • can you tell how to have invisible captcha for newsletter in magento 2? Commented Jun 1, 2020 at 21:38
1

Magento has a default option for google captcha in contact us page.

Stores -> Configuration -> Customers -> Customer Configuration -> CAPTCHA -> Choose Contact Us under Forms

Refer the screenshot below

answered May 2, 2019 at 11:29
4
  • is that google captcha? Commented May 2, 2019 at 11:31
  • Yes, it is. Magento has default option. Magento 2.3 version has google recaptcha Commented May 2, 2019 at 11:33
  • yes I have tried, but I don't think this is google captcha :) screencast.com/t/5nKBGA8d Commented May 2, 2019 at 11:44
  • Thanks for the answer. +1 :) Commented May 2, 2019 at 11:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.