Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="tr-div-cpf">
    <input  type="number" 
            id="tr-cpf" 
            placeholder="CPF SEM PONTO E SEM TRAÇO"
            onkeypress="return apenasNumeros(event)"
    >
    <button onclick="validaCPF()">VALIDA CPF</button>
</div>
              
            
!

CSS

              
                #tr-div-cpf,
    #tr-div-cpf input {
        width: 100%;
        margin-bottom: 25px;
    }

    #tr-div-cpf input {
        height: 50px;
        font-size: 1.5em;
    }

    #tr-div-cpf button {
        font-family: Ubuntu;
        font-size: 1.3em;
    }
              
            
!

JS

              
                function apenasNumeros(e) {
        let tecla = (window.event) ? event.keyCode : e.which;
        if ((tecla > 47 && tecla < 58)) 
            return true;
        else
            return false;
    }

    function validaCPF() {
        let cpf = document.getElementById('tr-cpf').value;        
        if(cpf === '') {
            alert('Favor informe um CPF!');
            limpaInput();
        } else if (cpf.length < 11 || cpf.length > 11) {
            alert('Favor informe um CPF válido!');
            limpaInput();
        } else {            
            let strCPF = cpf.toString();
            let counter = 0;

            //VERIFICA SE OS 11 DÍGITOS SÃO IGUAIS COMO POR EXEMPLO EM 11111111111
            for(let i=0; i < strCPF.length - 1; i++) {
                if(strCPF[i] == strCPF[i+1]) {
                    strIgual = true;
                    counter++;
                } else {
                    strIgual = false;
                }                
            }

            //VERIFICA SE O CONTADOR É IGUAL 10: SE SIM, ENTÃO TODOS OS NÚMEROS SÃO IGUAIS, SENÃO VERIFICA SE O CPF É VÁLIDO SEGUNDO A FÓRMULA DE CÁLCULO DO DÍGITO VERIFICADOR
            if (counter == 10) {
                alert('CPF inválido');
                limpaInput();
            } else {
                let soma = 0;
                let resto;
                let primeiroDigito;
                let segundoDigito;

                //BLOCO DE VERIFICAÇÃO DO PRIMEIRO DÍGITO VERIFICADOR
                for(let i=1; i <= strCPF.length - 2; i++) {
                    soma = soma + parseInt(strCPF.substring(i-1, i)) * (11 - i);                    
                }
                resto = (soma * 10) % 11;
                
                if ((resto === 10) || (resto === 11)) {
                    resto = 0;
                }

                if (resto !== parseInt(cpf.substring(9, 10))) {
                    alert('PRIMEIRO Dígito verificador inválido!');
                } else {
                    primeiroDigito = true;
                }

                //BLOCO DE VERIFICAÇÃO DO SEGUNDO DÍGITO VERIFICADOR
                soma = 0;
                if(primeiroDigito) {
                    for (let i = 1; i <= strCPF.length - 1; i++) {
                        soma = soma + parseInt(strCPF.substring(i-1, i)) * (12 - i);
                    }
                    resto = (soma * 10) % 11;

                    if ((resto === 10) || (resto === 11)) { 
                        resto = 0;
                    }

                    if (resto !== parseInt(cpf.substring(10, 11))) {
                        alert('SEGUNDO Dígito verificador inválido!');
                    } else {
                        segundoDigito = true;
                    }
                }

                if( primeiroDigito && segundoDigito) {
                    alert('CPF VÁLIDO!!!!');                    
                }
            }
            
        }
    }

    function limpaInput() {
        document.getElementById('tr-cpf').value = '';
    }
              
            
!
999px

Console