Validation of CNPJ with Angularjs

Asked

Viewed 12,782 times

3

I’ve already managed to make a mask, but I still can’t validate whether or not this cnpj exists.

What I have today is

 <input id="input-cnpj" name="cnpj" id="cnpj" ng-model="data.cnpj" type="tel" ui-mask="99.999.999/9999-99" ng-click="insereClass()" required>

Does anyone know how I could do this validation? I only found it in Jquery formats

  • 2

    You wonder if he exists or if it is a valid number?

  • 1

    And how so, found in jQuery formats? If you want to just validate, is a mathematical operation, just pure Javascript. For example: http://answall.com/questions/47033/valida%C3%A7%C3%A3o-cnpj-javascript

4 answers

7

You can use a library called Ngcpfcnpj.

Behold:

<input name="cpf" ng-model="cpf" ng-cpf ui-mask="999.999.999-99" />
myForm.cpf.$valid: {{ myForm.cpf.$valid }}
  • this library validates the checker digit or only the format? e.g.: 222.222.222-22 is not a valid Cpf, it would return true or false?

  • I tested this directive and it only seems to validate the format. Check?

2

1) Extract numbers using rgex

var cnpj = $("#input-cnpj").value;
cnpj = cnpj.replace(/[^\d]+/g,'');

2) validate the extracted value using the function below
reference: www.geradorderg.com/logica-verifier-cnpj

function validarCNPJ(input_cnpj){

 if(input_cnpj){
   var input=input_cnpj.toString();
   var pesos_A=[5,4,3,2,9,8,7,6,5,4,3,2];
   var pesos_B=[6,5,4,3,2,9,8,7,6,5,4,3,2];
   var sum=0;
   var x1=0;
   var x2=0;
   for(var i=0;i=2){
     x1=11-mod;
   }
   //calcula digito 2
   sum=0;
   for(var i=0;i=2){
     x2=11-mod;
   }

   //test digitos
   if(x1==input[12] && x2==input[13]){
     return true;
   }else{
     return false;
   }
 }else{
   return false;
 }
};

0

Have you tried using ngPattern(https://docs.angularjs.org/api/ng/directive/input), deta forma?

<input ng-pattern="/^\d{3}\.\d{3}\.\d{3}\-\d{2}$/" id="input-cnpj" name="cnpj" id="cnpj" ng-model="data.cnpj" type="tel" ui-mask="99.999.999/9999-99" ng-click="insereClass()" required>
  • 2

    This will only check if the user input is in the desired format, but, if the CNPJ is valid or not, that’s another story, and in this case, your code does not help anything, hence my vote against. Not to mention that his ng-pattern is in CPF format, not CNPJ.

0


According to the reply @bfavaretto quoted in the comments, here is an adaptation:

I am considering the code already inside a controller. And Angularjs can execute commands jQuery usually because it is done based on that library.

$("#input-cnpj").click(function() {
  var cnpj = $("#input-cnpj").value;
  cnpj = cnpj.replace(/[^\d]+/g,'');
  if (cnpj == '') return false;
  if (cnpj.length != 14)
      return false;
  // LINHA 10 - Elimina CNPJs invalidos conhecidos
  if (cnpj == "00000000000000" || 
      cnpj == "11111111111111" || 
      cnpj == "22222222222222" || 
      cnpj == "33333333333333" || 
      cnpj == "44444444444444" || 
      cnpj == "55555555555555" || 
      cnpj == "66666666666666" || 
      cnpj == "77777777777777" || 
      cnpj == "88888888888888" || 
      cnpj == "99999999999999")
      return false; // LINHA 21

  // Valida DVs LINHA 23 -
  tamanho = cnpj.length - 2
  numeros = cnpj.substring(0,tamanho);
  digitos = cnpj.substring(tamanho);
  soma = 0;
  pos = tamanho - 7;
  for (i = tamanho; i >= 1; i--) {
      soma += numeros.charAt(tamanho - i) * pos--;
      if (pos < 2)
          pos = 9;
  }
  resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
  if (resultado != digitos.charAt(0))
      return false;

  tamanho = tamanho + 1;
  numeros = cnpj.substring(0,tamanho);
  soma = 0;
  pos = tamanho - 7;
  for (i = tamanho; i >= 1; i--) {
      soma += numeros.charAt(tamanho - i) * pos--;
      if (pos < 2)
          pos = 9;
  }
  resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
  if (resultado != digitos.charAt(1))
        return false; // LINHA 49

  return true; // LINHA 51
});

Source of the function

Browser other questions tagged

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