Advertisement
Claudio51

Descubra os dígitos verificadores dos números e gere CPF

Jul 4th, 2019 (edited)
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 6.25 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3.     <head>
  4.         <title>descobrindo os números verificadores</title>
  5.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  6.         <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
  7.         <meta name="referrer" content="default" id="meta_referrer" />
  8.         <style>
  9.             body {
  10.                 font-family: Arial, Helvetica, sans-serif;
  11.                 background-color: #F2F5A9;
  12.                 font-size: 1em;
  13.             }
  14.             .div {
  15.                 position: absolute;
  16.                 background-color: #F7BE81;
  17.                 width: 90%;
  18.                 margin: 20px;
  19.                 padding: 10px 20px 20px 20px;
  20.                 border: 1px solid #ccc;
  21.             }
  22.             .button, .text {
  23.                 padding: 10px;
  24.                 text-align: center;
  25.             }
  26.             .text {
  27.                 width: 100px;
  28.             }
  29.             input[type="radio"] {
  30.                 -webkit-appearance: checkbox; /* Chrome, Safari, Opera */
  31.                 -moz-appearance: checkbox; /* Firefox */
  32.                 -ms-appearance: checkbox; /* not currently supported */
  33.             }
  34.             @media screen and (max-width: 480px) {
  35.                 .div {
  36.                     width: 78%;
  37.                     margin: 10px;
  38.                 }
  39.             }
  40.         </style>
  41.     </head>
  42.     <body>
  43.         <br>
  44.         <h3>
  45.         <center>
  46.             Descubra os dígitos verificadores e gere CPFs válidos
  47.         </center></h3>
  48.         <br>
  49.         <center>
  50.             <p>
  51.                 Digite os 9 números do cpf e descubra os dígitos verificadores
  52.             </p>
  53.             <input type="text" class="text" id="valorCpf" name="valorCpf" maxlength="9" autofocus onkeypress='return event.charCode >= 48 && event.charCode <= 57' />
  54.             <input type="button" class="button" name="gerarValor"value="Descobrir" onclick="gerarCPF(document.getElementById('cpf'));" />
  55.             <input type="reset" class="button" value="Limpar" onclick="limpa()" />
  56.  
  57.             <div class="div">
  58.                 <p>
  59.                     Pontuação
  60.                 </p>
  61.                 <label class="label_pontuacao" >
  62.                     <input type="radio" name="pontuacaoCpf" value="true" checked>
  63.                     Com pont.</label>
  64.                 <label class="label_pontuacao">
  65.                     <input type="radio" name="pontuacaoCpf" value="false">
  66.                     Sem pont.</label>
  67.                 <hr>
  68.                 <p>
  69.                     CPF completo
  70.                 </p>
  71.                 <input type="text" class="text" name="cpf" id="cpf" readonly="readonly"/>
  72.                 <p>
  73.                     <div id="estado">
  74.                         Mostra o estado do CPF aqui.
  75.                     </div>
  76.         </center>
  77.         </div>
  78.  
  79.         <script>
  80.             function gerarCPF(field) {
  81.                 // Pega o nome do input radio que vai usar navariavel pontuacaoCpf
  82.                 var pontuacaoCpf = document.getElementsByName("pontuacaoCpf");
  83.                 // Faz a varredura nos inputs radio com o nome do documento escolhido acima
  84.                 for ( a = 0; a < pontuacaoCpf.length; a++) {
  85.                     // Ao encontrar o input radio marcado, pega o valor da id dele
  86.                     if (pontuacaoCpf[a].checked) {
  87.                         // Breca a varredura pelos inputs radio
  88.                         break;
  89.                     }
  90.                 }
  91.                 var numerosCpf = document.getElementById('valorCpf').value;
  92.  
  93.                 if (numerosCpf != '000000000' &
  94.                     numerosCpf != '111111111' &
  95.                     numerosCpf != '222222222' &
  96.                     numerosCpf != '333333333' &
  97.                     numerosCpf != '444444444' &
  98.                     numerosCpf != '555555555' &
  99.                     numerosCpf != '666666666' &
  100.                     numerosCpf != '777777777' &
  101.                     numerosCpf != '888888888' &
  102.                     numerosCpf != '999999999')
  103.                     //numerosCpf != '123456789' &
  104.                     //numerosCpf != '987654321')
  105.                     {
  106.  
  107.                     if (numerosCpf != '' && numerosCpf.length == 9) {
  108.                         var comPontos = pontuacaoCpf[a].value;
  109.                         var n = 9;
  110.                         var n1 = numerosCpf.substring(0, 1);
  111.                         var n2 = numerosCpf.substring(1, 2);
  112.                         var n3 = numerosCpf.substring(2, 3);
  113.                         var n4 = numerosCpf.substring(3, 4);
  114.                         var n5 = numerosCpf.substring(4, 5);
  115.                         var n6 = numerosCpf.substring(5, 6);
  116.                         var n7 = numerosCpf.substring(6, 7);
  117.                         var n8 = numerosCpf.substring(7, 8);
  118.                         var n9 = numerosCpf.substring(8, 9);
  119.  
  120.                         if (n9 == 0) {
  121.                             document.getElementById('estado').innerHTML = "Rio Grande do Sul";
  122.                         } else if (n9 == 1) {
  123.                             document.getElementById('estado').innerHTML = "Distrito Federeal, Goiás, Mato Grosso do Sul e Tocantins";
  124.                         } else if (n9 == 2) {
  125.                             document.getElementById('estado').innerHTML = "Pará, Amazonas, Acre, Amapá, Rondônia e Roraima";
  126.                         } else if (n9 == 3) {
  127.                             document.getElementById('estado').innerHTML = "Ceará, Maranhão e Piauí";
  128.                         } else if (n9 == 4) {
  129.                             document.getElementById('estado').innerHTML = "Pernambuco, Rio Grande do Norte, Paraíba e Alagoas";
  130.                         } else if (n9 == 5) {
  131.                             document.getElementById('estado').innerHTML = "Bahia e Sergipe";
  132.                         } else if (n9 == 6) {
  133.                             document.getElementById('estado').innerHTML = "Minas Gerais";
  134.                         } else if (n9 == 7) {
  135.                             document.getElementById('estado').innerHTML = "Rio de Janeiro e Espírito Santo";
  136.                         } else if (n9 == 8) {
  137.                             document.getElementById('estado').innerHTML = "São Paulo";
  138.                         } else if (n9 == 9) {
  139.                             document.getElementById('estado').innerHTML = "Paraná e Santa Catarina";
  140.                         };
  141.                        
  142.                         var d1 = n9 * 2 + n8 * 3 + n7 * 4 + n6 * 5 + n5 * 6 + n4 * 7 + n3 * 8 + n2 * 9 + n1 * 10;
  143.                         d1 = 11 - ( mod(d1, 11) );
  144.                         if (d1 >= 10)
  145.                             d1 = 0;
  146.                         var d2 = d1 * 2 + n9 * 3 + n8 * 4 + n7 * 5 + n6 * 6 + n5 * 7 + n4 * 8 + n3 * 9 + n2 * 10 + n1 * 11;
  147.                         d2 = 11 - ( mod(d2, 11) );
  148.                         if (d2 >= 10)
  149.                             d2 = 0;
  150.                         retorno = '';
  151.                         if (comPontos == 'true')
  152.                             cpf = '' + n1 + n2 + n3 + '.' + n4 + n5 + n6 + '.' + n7 + n8 + n9 + '-' + d1 + d2;
  153.                         else
  154.                             cpf = '' + n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8 + n9 + d1 + d2;
  155.                         field.value = cpf;
  156.                     } else {
  157.                         document.getElementById('cpf').value = '';
  158.                     }
  159.                 } else {
  160.                     document.getElementById('valorCpf').value = '';
  161.                     document.getElementById('valorCpf').focus();
  162.                     document.getElementById('cpf').value = "";
  163.                     document.getElementById('estado').innerHTML = "Número para verificação inválido.";
  164.                 };
  165.             }
  166.  
  167.             function randomiza(n) {
  168.                 var ranNum = Math.round(Math.random() * n);
  169.                 return ranNum;
  170.             }
  171.  
  172.             function mod(dividendo, divisor) {
  173.                 return Math.round(dividendo - (Math.floor(dividendo / divisor) * divisor));
  174.             }
  175.  
  176.             function limpa() {
  177.                 document.getElementById('valorCpf').value = "";
  178.                 document.getElementById('cpf').value = "";
  179.                 document.getElementById('valorCpf').focus();
  180.                 document.getElementById('estado').innerHTML = "Mostra o estado do CPF aqui.";
  181.             }
  182.  
  183.         </script>
  184.     </body>
  185. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement