Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable submit button according to the response of the recaptcha user Vuejs

My goal is that a form button is enabled, if the user correctly responded to the recaptcha, I already found a solution using pure javascript (stackoverflow solution), but I need to do it with VueJs, when using the data-callback attribute of recaptcha, it indicates an error, the which says: "ReCAPTCHA could not find user-provided function: responseRC ()", with which I can not get the answer and activate the button.

My code - HTML

<div id='app'>
  <label for="name">
    <input type="text" name="name" v-model="namex">
  </label>
  <label for="agree">
    <input id="agree" type="checkbox" value="agree" />I Agree</lable>
  <p>
    <hr>
   <div class="g-recaptcha" data-sitekey="6Ld8BDUU0000Edp4MdRqV24fKJe8llSzLfiEQEH" data-callback="responseRC()" ></div>
  <hr>
  <button :name="buttonX" disabled>Button</button>
</div>

Code Vue JS

var ins = new Vue({
      el : '#app',
      data: {
        //checked : false,
        namex: 'Javier',
        buttonX: ''
      },
      mounted: function() {
        this.buttonX.disabled;
        console.log('monta algo');

      },
      beforeupdated: function(){
        console.log('cambia algo');
        this.buttonX.disabled.false;
      },

      methods: {
        disableButton: function(){
            this.buttonX.disabled=false;
        },
        responseRC: function(){
            console.log("The captcha has been already solved");
            if(grecaptcha.getResponse().length !== 0){
               console.log("The captcha has been already solved");
            }
        }

      }
    });
like image 401
JavierMD Avatar asked Oct 27 '25 05:10

JavierMD


1 Answers

In a nutshell, you have to use @verify, inside the method it'll refer to toggle the boolean flag and include this in the :disabled of a button.

The logic I got (Vuetify/Class API/TypeScript syntax):

in html (the ref you might skip, I'm using this to reset reCAPTCHA on reset button click)

<vue-recaptcha
  @verify="recaptchaVerified"
  ref="recaptcha"
  sitekey="ABC..."
></vue-recaptcha>

:disabled on a button (note the isRecaptchaValid flag)

<v-btn
  :disabled="!isFormValid || !isRecaptchaValid"
  text
  color="purple darken-2"
  type="submit"
  >Send</v-btn
>

TypeScript logic

public isRecaptchaValid: boolean = false;
public recaptchaVerified(response: any): void {
  this.isRecaptchaValid = true;
}
like image 107
Daniel Danielecki Avatar answered Oct 28 '25 18:10

Daniel Danielecki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!