Validation of CNPJ with angularjs

3

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

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 just found it in Jquery formats

    
asked by anonymous 09.02.2015 / 20:53

4 answers

-1

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

I'm considering the code already inside a controller. And AngularJS can execute commands from jQuery normally 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
});

Function Font

    
12.02.2015 / 11:52
7

You can use a library called NgCpfCnpj .

See:

<input name="cpf" ng-model="cpf" ng-cpf ui-mask="999.999.999-99" />
myForm.cpf.$valid: {{ myForm.cpf.$valid }}
    
19.09.2016 / 21:37
2

1) Extract the 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-verter-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;
 }
};
    
09.06.2015 / 02:26
0

Have you tried using ngPattern ( link ), deta form?

<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>
    
11.02.2015 / 19:03