How to run reCaptcha ONLY if HTML5 validation has passed?

前端 未结 2 1026
南方客
南方客 2020-12-24 13:22

Usually, HTML5 form validation runs before the submit event.

With this

2条回答
  •  盖世英雄少女心
    2020-12-24 14:09

    maechler's answer is excellent, however, if one does not want to use jQuery, you can add the event listener with an iife

    (function() {
        document.getElementById("my-form").addEventListener("submit", function(event) {
            console.log('form submitted.');
            if (!grecaptcha.getResponse()) {
                console.log('captcha not yet completed.');
    
                event.preventDefault(); //prevent form submit
                grecaptcha.execute();
            } else {
                console.log('form really submitted.');
            }
          });
      })();
    

    I hope that helps.

提交回复
热议问题