20
I’m working with a CPF validation on my controller, but I need to validate when the customer leaves the input of the CPF and then return a message warning when the CPF is incorrect, through a alert or something like that.
I tried to do something like this, but I get an error message on the Chrome console:
Uncaught Referenceerror: validarCPF is not definedRelate:363 onblur
<script>
    function validarCPF() {
        if (vercpf(document.frmcpf.cpf.value))
        {
            document.frmcpf.submit();
        } else {
            errors = "1";
            if (errors)
                alert('CPF inválido');
            document.retorno = (errors == '');
        }
    }
    function vercpf(cpf) {
        if (cpf.length != 11 ||
            cpf == "00000000000" ||
            cpf == "11111111111" ||
            cpf == "22222222222" ||
            cpf == "33333333333" ||
            cpf == "44444444444" ||
            cpf == "55555555555" ||
            cpf == "66666666666" ||
            cpf == "77777777777" ||
            cpf == "88888888888" ||
            cpf == "99999999999")
            return false;
        add = 0;
        for (i = 0; i < 9; i++)
                add += parseInt(cpf.charAt(i)) * (10 - i);
        rev = 11 - (add % 11);
        if (rev == 10 || rev == 11)
            rev = 0;
        if (rev != parseInt(cpf.charAt(9)))
            return false;
        add = 0;
                for (i = 0; i < 10; i++)
                add += parseInt(cpf.charAt(i)) * (11 - i);
        rev = 11 - (add % 11);
        if (rev == 10 || rev == 11)
            rev = 0;
        if (rev != parseInt(cpf.charAt(10)))
            return false;
        alert('O CPF INFORMADO É VÁLIDO.');
        return true;
    }
    $j(document).ready(function () {
        $j("#meuForm").validate({
            rules: {
                NrCpf: {NrCpf: true, required: true}
            },
            messages: {
                NrCpf: {NrCpf: alert('CPF Inválido')}
            }
        });
    });
</script>
Input HTML:
<form id="meuform">
    <input type="text" data-id="NrCpf" disabled name="NrCpf" id="NrCpf" class="form-control" onblur="javascript: validarCPF(this.value);" maxlength="14">
</form>
Has anyone ever had to do anything like that, or have any idea how to help me?
This link shows how to validate a CPF while the person is typing it http://www.tutsup.com/2014/11/17/como-validar-cpf-e-cnpj-com-javascript-ou-jquery/ . I think this is what you’re looking for
– Eduardo
The script has been declared before of
input?– André Ribeiro
@Eduardo I downloaded the project on the site, but it has an error on the line *? if ( valida_cpf_cnpj( cpf_cnpj ) {** and is not working here.
– Randrade
@Andréribeiro no, I just reversed the order of the post. The script is in JS file separately, I just joined to post.
– Randrade