Created
October 30, 2014 17:29
-
-
Save rdccosmo/959ace12da269aa685fb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//valida o CPF digitado | |
function validaCPF(cpf) | |
{ | |
erro = new String; | |
if (cpf.val().length == 11) | |
{ | |
cpf.val(cpf.val().replace('.', '')); | |
cpf.val(cpf.val().replace('.', '')); | |
cpf.val(cpf.val().replace('-', '')); | |
var nonNumbers = /\D/; | |
if (nonNumbers.test(cpf.val())) | |
{ | |
erro = "A verificacao de CPF suporta apenas números!"; | |
} | |
else | |
{ | |
if (cpf.val() == "00000000000" || | |
cpf.val() == "11111111111" || | |
cpf.val() == "22222222222" || | |
cpf.val() == "33333333333" || | |
cpf.val() == "44444444444" || | |
cpf.val() == "55555555555" || | |
cpf.val() == "66666666666" || | |
cpf.val() == "77777777777" || | |
cpf.val() == "88888888888" || | |
cpf.val() == "99999999999") { | |
erro = "Número de CPF inválido!" | |
} | |
var a = []; | |
var b = new Number; | |
var c = 11; | |
for (i=0; i<11; i++){ | |
a[i] = cpf.val().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; } | |
if ((cpf.val().charAt(9) != a[9]) || (cpf.val().charAt(10) != a[10])) { | |
erro = "Número de CPF inválido."; | |
} | |
} | |
} | |
else | |
{ | |
if(cpf.val().length == 0) | |
return {result: false, success: erro}; | |
else | |
erro = "Número de CPF inválido."; | |
} | |
if (erro.length > 0) { | |
cpf.focus(); | |
return {result: false, success: erro}; | |
} | |
return {result: true, success: erro}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment