JS/Vue CPF validator

Asked

Viewed 687 times

-2

I’m trying to use a function to validate CPF and at the end of it call the function that checks whether the field is required and numerical.

If I call only the function forgotmain, all goes well, but does not validate the CPF. I am using v-btn @click.prevent="validarCPF()" in HTML to call functions.

methods: {
    forgotmain() {
      if (!this.$v.$invalid) {
        this.$router.push("forgotcode");
      } else {
        this.$v.$touch();
      }
    },
    validarCPF(inputCPF) {
      var soma = 0;
      var i;
      var resto;
      var inputCPF = document.getElementById("cpf").value;

      if (inputCPF == "00000000000") return false;
      for (i = 1; i <= 9; i++)
        soma = soma + parseInt(inputCPF.substring(i - 1, i)) * (11 - i);
      resto = (soma * 10) % 11;

      if (resto == 10 || resto == 11) resto = 0;
      if (resto != parseInt(inputCPF.substring(9, 10))) return false;

      soma = 0;
      for (i = 1; i <= 10; i++)
        soma = soma + parseInt(inputCPF.substring(i - 1, i)) * (12 - i);
      resto = (soma * 10) % 11;

      if (resto == 10 || resto == 11) resto = 0;
      if (resto != parseInt(inputCPF.substring(10, 11))) return false;
      return true;

      forgotmain();
    },
}

1 answer

0

At the end of your function, you are calling forgotMain as follows

forgotmain()

change to:

this.forgotmain()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.