captcha form validation required error message in vue-recaptcha

前端 未结 1 1658
猫巷女王i
猫巷女王i 2021-01-13 02:57

I am following this Package in vue.js and Laravel 5.6.7 to implement captcha.

https://github.com/DanSnow/vue-recaptcha#install

Component code in vue.

相关标签:
1条回答
  • 2021-01-13 03:33

    You can use a property (loginForm.recaptchaVerified below) to track if the recaptcha was verified and prevent submit + display a message if not:

    JSFiddle demo: https://jsfiddle.net/acdcjunior/o7aca7sn/3/

    Code below:

    Vue.component('vue-recaptcha', VueRecaptcha);
    
    new Vue({
      el: '#app',
      data: {
        loginForm: {
          recaptchaVerified: false,
          pleaseTickRecaptchaMessage: ''
        }
      },
      methods: {
        markRecaptchaAsVerified(response) {
          this.loginForm.pleaseTickRecaptchaMessage = '';
          this.loginForm.recaptchaVerified = true;
        },
        checkIfRecaptchaVerified() {
          if (!this.loginForm.recaptchaVerified) {
            this.loginForm.pleaseTickRecaptchaMessage = 'Please tick recaptcha.';
            return true; // prevent form from submitting
          }
          alert('form would be posted!');
        }
      }
    })
    
    <script src="https://www.google.com/recaptcha/api.js?onload=vueRecaptchaApiLoaded&render=explicit" async defer>
    </script>
    <script src="https://unpkg.com/vue"></script>
    <script src="https://unpkg.com/vue-recaptcha@latest/dist/vue-recaptcha.js"></script>
    
    <div id="app">
      <form v-on:submit.prevent="checkIfRecaptchaVerified">
         <div>
            <vue-recaptcha @verify="markRecaptchaAsVerified"
                sitekey="6Le-wvkSAAAAAPBMRTvw0Q4Muexq9bi0DJwx_mJ-">
            </vue-recaptcha>
         </div>
         Some other fields of the form here...
         <br>
         <button>Submit form</button>
         <hr>
         <div><strong>{{ loginForm.pleaseTickRecaptchaMessage }}</strong></div>
      </form>
    </div>
    
    0 讨论(0)
提交回复
热议问题