Validate if input is actually CNPJ

Asked

Viewed 3,245 times

3

I have a form and I need to block the number entry in this.

    <div id="document" class="form-group">
       <input type="text" name="document"  class="form-control" placeholder="CNPJ da Empresa" />
       <label for="document" class="text-danger">Informe um documento válido</label>
       <i class="fa fa-2x form-control-feedback"></i>
    </div>

from this form, the information is sent to the registration page you receive as follows:

  header("Access-Control-Allow-Origin: *");
  $document = addslashes($_POST["document"]);

My problem is: how to block CPF input if possible in the form yet?

I tried to make a strlen to check if the size is greater than 11 (number amount of CPF), but it always brings the return as 0 (zero).

if (strlen(document) > 11) {
  $document = addslashes($_POST["document"]);
}

help please

Sincerely yours

  • 1
  • For php what comes, are the <input> that are inside a form and have the attribute name id does not go to php unless you are using JS.

  • Aff answered all the paranauê, but I forgot to ask. Where is the form? There is no code. That would not be the problem?

  • sorry, I didn’t. <form action="Register.php" role="form" method="post" name="name" >

1 answer

2

PHP Library for Validation

A good library to validate is the Respect\Validation.

Javascript validation

Another option is to use the function generated by the Gerador de CNPJ. Just that it’s in javascript

http://www.geradorcnpj.com/javascript-validar-cnpj.htm

function validarCNPJ(cnpj) {
 
    cnpj = cnpj.replace(/[^\d]+/g,'');
 
    if(cnpj == '') return false;
     
    if (cnpj.length != 14)
        return false;
 
    // 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;
         
    // Valida DVs
    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;
           
    return true;
    
}

You might want to take a shot at that javascript there if you have a better notion of javascript.

Validation in PHP

Here follows a function to validate cnpjin PHP:

function validar_cnpj($cnpj)
{
    $cnpj = preg_replace('/[^0-9]/', '', (string) $cnpj);
    // Valida tamanho
    if (strlen($cnpj) != 14)
        return false;
    // Valida primeiro dígito verificador
    for ($i = 0, $j = 5, $soma = 0; $i < 12; $i++)
    {
        $soma += $cnpj{$i} * $j;
        $j = ($j == 2) ? 9 : $j - 1;
    }
    $resto = $soma % 11;
    if ($cnpj{12} != ($resto < 2 ? 0 : 11 - $resto))
        return false;
    // Valida segundo dígito verificador
    for ($i = 0, $j = 6, $soma = 0; $i < 13; $i++)
    {
        $soma += $cnpj{$i} * $j;
        $j = ($j == 2) ? 9 : $j - 1;
    }
    $resto = $soma % 11;
    return $cnpj{13} == ($resto < 2 ? 0 : 11 - $resto);
}

Source: https://gist.github.com/guisehn/3276302

  • do not use this solution, this super outdated and some cnpjs will not validate.

  • Do you have an up-to-date answer? If so, you can reply here to us.

Browser other questions tagged

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