Created
February 16, 2017 20:01
-
-
Save rbenvenuto/bc0f8f9a784f19e25774d4c9d46658c9 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
<script type="text/javascript"> | |
document.getElementById('username').onblur = function validarCPF(cpf){ | |
var cpf = document.getElementById('username').value; | |
var cpfStr = cpf.toString(); | |
if(cpfStr.length != 11 || cpfStr.replace(eval('/'+cpfStr.charAt(1)+'/g'),'') == '') | |
{ | |
alert('CPF inválido'); | |
} | |
else | |
{ | |
for(n=9; n<11; n++) | |
{ | |
for(d=0, c=0; c<n; c++) d += cpfStr.charAt(c) * ((n + 1) - c); | |
d = ((10 * d) % 11) % 10; | |
if(cpfStr.charAt(c) != d) return false; | |
} | |
return true; | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment