Skip to content

Instantly share code, notes, and snippets.

@willnss
Forked from leonardofaria/gist:1037052
Created August 3, 2012 10:52
Show Gist options
  • Save willnss/3246637 to your computer and use it in GitHub Desktop.
Save willnss/3246637 to your computer and use it in GitHub Desktop.
Gerador de CPF, NIT, CNPJ e CEI válidos
<html>
<head>
<title>Gerador de CPF, CNPJ e NIT Válidos</title>
<script language="javascript">
function gera_random(n) {
var ranNum = Math.round(Math.random()*n);
return ranNum;
}
function mod(dividendo,divisor) {
return Math.round(dividendo - (Math.floor(dividendo/divisor)*divisor));
}
function cpf() {
var n = 9;
var n1 = gera_random(n);
var n2 = gera_random(n);
var n3 = gera_random(n);
var n4 = gera_random(n);
var n5 = gera_random(n);
var n6 = gera_random(n);
var n7 = gera_random(n);
var n8 = gera_random(n);
var n9 = gera_random(n);
var d1 = n9*2+n8*3+n7*4+n6*5+n5*6+n4*7+n3*8+n2*9+n1*10;
d1 = 11 - (mod(d1,11));
if (d1>=10) d1 = 0;
var d2 = d1*2+n9*3+n8*4+n7*5+n6*6+n5*7+n4*8+n3*9+n2*10+n1*11;
d2 = 11 - (mod(d2,11));
if (d2>=10) d2 = 0;
if (document.form1.mascara.checked)
return ''+n1+n2+n3+'.'+n4+n5+n6+'.'+n7+n8+n9+'-'+d1+d2;
else
return ''+n1+n2+n3+n4+n5+n6+n7+n8+n9+d1+d2;
}
function cnpj() {
var n = 9;
var n1 = gera_random(n);
var n2 = gera_random(n);
var n3 = gera_random(n);
var n4 = gera_random(n);
var n5 = gera_random(n);
var n6 = gera_random(n);
var n7 = gera_random(n);
var n8 = gera_random(n);
var n9 = 0;//gera_random(n);
var n10 = 0;//gera_random(n);
var n11 = 0;//gera_random(n);
var n12 = 1;//gera_random(n);
var d1 = n12*2+n11*3+n10*4+n9*5+n8*6+n7*7+n6*8+n5*9+n4*2+n3*3+n2*4+n1*5;
d1 = 11 - ( mod(d1,11) );
if (d1>=10) d1 = 0;
var d2 = d1*2+n12*3+n11*4+n10*5+n9*6+n8*7+n7*8+n6*9+n5*2+n4*3+n3*4+n2*5+n1*6;
d2 = 11 - ( mod(d2,11) );
if (d2>=10) d2 = 0;
if (document.form1.mascara.checked)
return ''+n1+n2+'.'+n3+n4+n5+'.'+n6+n7+n8+'/'+n9+n10+n11+n12+'-'+d1+d2;
else
return ''+n1+n2+n3+n4+n5+n6+n7+n8+n9+n10+n11+n12+d1+d2;
}
function nit() {
var n = 9;
var n1 = 1;//gera_random(n);
var n2 = gera_random(n);
var n3 = gera_random(n);
var n4 = gera_random(n);
var n5 = gera_random(n);
var n6 = gera_random(n);
var n7 = gera_random(n);
var n8 = gera_random(n);
var n9 = gera_random(n);
var n10 = gera_random(n);
var d1 = n1*3 + n2*2 + n3*9 + n4*8 + n5*7 + n6*6 + n7*5 + n8*4 + n9*3 + n10*2;
d1 = 11 - ( mod(d1,11) );
if (d1>=10) d1 = 0;
if (document.form1.mascara.checked)
return ''+n1+n2+n3+'.'+n4+n5+n6+n7+n8+'.'+n9+n10+'-'+d1;
else
return ''+n1+n2+n3+n4+n5+n6+n7+n8+n9+n10+d1;
}
function cei() {
var n = 9;
var n1 = 2; // deve ser diferente de 0
var n2 = gera_random(n);
var n3 = gera_random(n);
var n4 = gera_random(n);
var n5 = gera_random(n);
var n6 = gera_random(n);
var n7 = gera_random(n);
var n8 = gera_random(n);
var n9 = gera_random(n);
var n10 = gera_random(n);
var n11 = 8; // atividade
var aux1 = n1*7 + n2*4 + n3*1 + n4*8 + n5*5 + n6*2 + n7*1 + n8*6 + n9*3 + n10*7 + n11 * 4;
var aux2 = aux1 + '';
var aux3 = parseInt(aux2[aux2.length - 1]) + parseInt(aux2[aux2.length - 2]);
var Soma = parseInt(aux1);
var d1 = parseInt((10 - (Soma % 10 + parseInt(Soma / 10)) % 10) % 10);
d1 = Math.abs(d1);
if (document.form1.mascara.checked)
return ''+n1+n2+'.'+n3+n4+n5+'.'+n6+n7+n8+n9+n10+'/'+n11+d1;
else
return ''+n1+n2+n3+n4+n5+n6+n7+n8+n9+n10+n11+d1;
}
function gera() {
if (document.form1.tipo[0].checked)
document.form1.numero.value = cpf();
else if (document.form1.tipo[1].checked)
document.form1.numero.value = cnpj();
else if (document.form1.tipo[2].checked)
document.form1.numero.value = nit();
else if (document.form1.tipo[3].checked)
document.form1.numero.value = cei();
}
</script>
</head>
<body>
<h1>Gerador de CPF, NIT, CNPJ, CEI válidos</h1>
<form name="form1" method="post" action="">
<label for="numero">Número:</label> <input name="numero" id="numero" size="20" type="text" /><br/>
Tipo:
<input name="tipo" value="cpf" checked="checked" type="radio"> <label for="cpf">CPF</label>
<input name="tipo" value="cnpj" id="cnpj" type="radio" /> <label for="cnpf">CNPJ</label>
<input name="tipo" value="nit" id="nit" type="radio"> <label for="nit">NIT</label>
<input name="tipo" value="cei" id="cei" type="radio"> <label for="cei">CEI</label><br/>
<input name="mascara" value="mascara" id="mascara" type="checkbox"> <label for="mascara">Máscara</label><br/><br/>
<input name="button" value="Gerar" onclick="gera()" type="button">
</form>
</body>
</html>
@agbottan
Copy link

agbottan commented May 5, 2021

Obrigado! Me ajudou aqui.

@jadsongmatos
Copy link

function cnpj(input) {
  input = String(input);
  let c = Number(input[1]),
    d = Number(input[2]),
    e = Number(input[3]),
    f = Number(input[4]),
    g = Number(input[5]),
    h = Number(input[6]),
    k = Number(input[7]),
    l = Number(input[8]),
    m = Number(input[9]),
    n = Number(input[10]),
    p = Number(input[11]);
  input = Number(input[12]);
  let b =
    11 -
    ((2 * input +
      3 * p +
      4 * n +
      5 * m +
      6 * l +
      7 * k +
      8 * h +
      9 * g +
      2 * f +
      3 * e +
      4 * d +
      5 * c) %
      11);

  10 <= b && (b = 0);
  let q =
    11 -
    ((2 * b +
      3 * input +
      4 * p +
      5 * n +
      6 * m +
      7 * l +
      8 * k +
      9 * h +
      2 * g +
      3 * f +
      4 * e +
      5 * d +
      6 * c) %
      11);

  10 <= q && (q = 0);
  return "" + c + d + e + f + g + h + k + l + m + n + p + input + b + q;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment