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?

4
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>
3
  • Thanks for the answer. +1 and accepted :) May 2 '19 at 11:45
  • Excellent and working very fine. +1 Jun 1 '20 at 21:38
  • can you tell how to have invisible captcha for newsletter in magento 2? Jun 1 '20 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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.