How can I make reCAPTCHA a required field?

前端 未结 7 1214
闹比i
闹比i 2020-12-01 05:34

I\'m using Google reCAPTCHA and have been able to add the CAPTCHA component to my page inside a form. But when I submit the form there\'s no validation taking place to check

相关标签:
7条回答
  • 2020-12-01 06:02

    I found this to be a quick & easy way to do it. Add this to your headers:

    <script>
    window.onload = function() {
      var recaptcha = document.forms["myForm"]["g-recaptcha-response"];
      recaptcha.required = true;
      recaptcha.oninvalid = function(e) {
        // do something
        alert("Please complete the captcha");
      }
    }
    </script>
    

    This only works in HTML5, and is (or should be) supported by these browsers: http://caniuse.com/#feat=form-validation

    (The JS console in Chrome shows this error message: "Invalid form control" only in Google Chrome , and I haven't been able to work around this. Hopefully someone else will improve this response.)

    0 讨论(0)
提交回复
热议问题