Skip to content

Instantly share code, notes, and snippets.

@abeswz
Last active August 24, 2016 13:20
Show Gist options
  • Save abeswz/ba18ced5b3df134ab5ce37f257ebb6a2 to your computer and use it in GitHub Desktop.
Save abeswz/ba18ced5b3df134ab5ce37f257ebb6a2 to your computer and use it in GitHub Desktop.
window.ParsleyConfig = {
validators: {
cpf: {
fn: function(val, req) {
val = $.trim(val);
val = val.replace('.', '');
val = val.replace('.', '');
cpf = val.replace('-', '');
while (cpf.length < 11) cpf = "0" + cpf;
var expReg = /^0+$|^1+$|^2+$|^3+$|^4+$|^5+$|^6+$|^7+$|^8+$|^9+$/;
var a = [];
var b = new Number;
var c = 11;
for (i = 0; i < 11; i++) {
a[i] = cpf.charAt(i);
if (i < 9) b += (a[i] * --c);
}
if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11 - x }
b = 0;
c = 11;
for (y = 0; y < 10; y++) b += (a[y] * c--);
if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11 - x; }
var result = true;
if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10]) || cpf.match(expReg)) result = false;
return result;
},
priority: 32
},
cep: {
fn: function(val, req) {
var re = /^\b(\d)\1+\b$/;
return !re.test(val.replace('-', ''));
}
},
cnpj: {
fn: function(val, req) {
var cnpj = val;
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;
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment