Skip to content

Instantly share code, notes, and snippets.

@jsvini
Last active August 29, 2015 14:05
Show Gist options
  • Save jsvini/a8a5d0c1146a028bd6e5 to your computer and use it in GitHub Desktop.
Save jsvini/a8a5d0c1146a028bd6e5 to your computer and use it in GitHub Desktop.
Validador de CPF com dígito verificador
// Valida CPF incluindo dígito verificador, retorna true ou false
// Formatos: 000.000.000.00 - 000.000.000-00 - 000,000,000.00 - 000,000,000-00 - 00000000000
// Compatibilidade: Chrome - Opera - Safari - Firefox >= 1.8 - IE >= 9
function ValidarCPF(a) {
var b, c, d, e, f;
a += "";
if (/[0-9]{3}(\.|\,)[0-9]{3}(\.|\,)[0-9]{3}(\-|\.)[0-9]{2}/.test(a)) a = a.replace(/[^\d]/g, "");
else if (!/[0-9]{11}/.test(a)) return !1;
b = a.substr(0, 9);
f = a.substr(9);
b = b.split("").map(function(a) {
return parseInt(a, 10)
});
c = 0;
b.forEach(function(a, b) {
e = a * (10 - b);
c += e
});
d = c % 11;
a = 2 > d ? 0 : 11 - d;
b = b.slice();
b.push(a);
c = 0;
b.forEach(function(a, b) {
e = a * (11 - b);
c += e
});
d = c % 11;
return a + "" + (2 > d ? 0 : 11 - d) === f
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment