Rodrigo, here is the correct function.
Put it in your file and call it by clicking on the element with the onclick event, if you do not know anything about JavaScript events, read this here: COMPLETE LIST OF JAVASCRIPT EVENTS
Its function only generated the random numbers, that is, it only did a part of the work, did not concatenate the values later.
Full example, it will generate a valid number as soon as you click on the input:
<!DOCTYPE HTML>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<script type="text/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 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;
resultado = ''+n1+n2+'.'+n3+n4+n5+'.'+n6+n7+n8+'/'+n9+n10+n11+n12+'-'+d1+d2;
document.getElementById('cnpj').value=resultado;
}
</script>
<title></title>
</head>
<body>
<input type="text" id="cnpj" onclick="cnpj();">
</body>
</html>
Here's just the function:
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 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;
resultado = ''+n1+n2+'.'+n3+n4+n5+'.'+n6+n7+n8+'/'+n9+n10+n11+n12+'-'+d1+d2;
document.getElementById('cnpj').value=resultado;
}