Using jQuery Validation Engine and validation of CNPJ

7

I'm using the jQuery Validation Engine with this file of translations into Portuguese. I have added the following line to the JavaScript:

"cnpj": {
    "regex": /^\d{2}\.\d{3}\.\d{3}\/\d{4}\-\d{2}$/,
     "alertText": "* CNPJ inválido"
 }

In ASPX I use:

asp:TextBox ID="txtCNPJ" runat="server" CssClass="validate[required,custom[cnpj]]

But this validation is not very efficient, since it only checks the number of characters.

How can I make a method to validate the CNPJ and continue using the Validation Engine in this way?

    
asked by anonymous 25.02.2014 / 14:14

3 answers

3

The right solution follows:

Placing in aspx

asp:TextBox ID="txtCNPJ" runat="server" CssClass="validate[required, funcCall[validateCNPJ]]"

Place in jquery.validationEngine-pt_BR.js

"cnpj": {
          "alertText": "* CNPJ inválido"
       }

Place in Javascript

function validateCNPJ(field, rules, i, options) {
    var valido = isCNPJValid(field.val()); //implementar a validação
    if (!valido) {
        //internacionalização
        return options.allrules.cnpj.alertText;
    }
}

function isCNPJValid(cnpj) {
    cnpj = cnpj.replace(/[^\d]+/g, '');
    if (cnpj == '') return false;
    if (cnpj.length != 14)
        return false;
    // Elimina CNPJs invalidos conhecidos
    if (cnpj == "00000000000000" ||
        cnpj == "11111111111111" ||
        cnpj == "22222222222222" ||
        cnpj == "33333333333333" ||
        cnpj == "44444444444444" ||
        cnpj == "55555555555555" ||
        cnpj == "66666666666666" ||
        cnpj == "77777777777777" ||
        cnpj == "88888888888888" ||
        cnpj == "99999999999999")
        return false;

    // Valida DVs
    tamanho = cnpj.length - 2
    numeros = cnpj.substring(0, tamanho);
    digitos = cnpj.substring(tamanho);
    soma = 0;
    pos = tamanho - 7;
    for (i = tamanho; i >= 1; i--) {
        soma += numeros.charAt(tamanho - i) * pos--;
        if (pos < 2)
            pos = 9;
    }
    resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
    if (resultado != digitos.charAt(0))
        return false;

    tamanho = tamanho + 1;
    numeros = cnpj.substring(0, tamanho);
    soma = 0;
    pos = tamanho - 7;
    for (i = tamanho; i >= 1; i--) {
        soma += numeros.charAt(tamanho - i) * pos--;
        if (pos < 2)
            pos = 9;
    }
    resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
    if (resultado != digitos.charAt(1))
        return false;

    return true;
}
    
25.02.2014 / 15:58
4

Looking at the documentation, I found funcCall[methodName] , which lets you call a function to perform validation.

Following is an example based on the documentation:

function verificaCNPJ(field, rules, i, options){
    var valido = isCNPJValid(field.val()); //implementar a validação
    if (!valido) {
        //internacionalização
        return options.allrules.validate2fields.alertText;
    }
}

The html should look like this:

<input value="" class="validate[required,funcCall[verificaCNPJ]]" type="text" id="cnpj" name="cnpj" />

To implement the CNPJ function, you can use the example of the Wikipedia :

function isCNPJValid() {  
    var b = [6,5,4,3,2,9,8,7,6,5,4,3,2], c = this;
    if((c = c.replace(/[^\d]/g,"").split("")).length != 14)
        return false;
    for (var i = 0, n = 0; i < 12; n += c[i] * b[++i]); 
    if(c[12] != (((n %= 11) < 2) ? 0 : 11 - n))
        return false; 
    for (var i = 0, n = 0; i <= 12; n += c[i] * b[i++]); 
    if(c[13] != (((n %= 11) < 2) ? 0 : 11 - n))
        return false; 
    return true; 
};
    
25.02.2014 / 14:41
0

Here's a good explanation of CNPJ validation, you can do it both on the front and backend.

link

    
25.02.2014 / 14:45