-
-
Save edyonil/5680229 to your computer and use it in GitHub Desktop.
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script> | |
<script class="jsbin" src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js" type="text/javascript"></script> | |
<script src="jquery.validate.cpf.js"></script> | |
<script type="text/javascript"> | |
$(function () { | |
var $cpf = $("#id_cpf").attr("name"); | |
var $params = {debug:false, rules:{}, messages:{}}; | |
$params['rules'][$cpf] = "cpf"; | |
$params['messages'][$cpf] = "CPF inválido"; | |
$("#frm").validate($params); | |
}); | |
</script> | |
<title>JS Bin</title> | |
<!--[if IE]> | |
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script> | |
<![endif]--> | |
<style> | |
article, aside, figure, footer, header, hgroup, menu, nav, section { display: block; } | |
</style> | |
</head> | |
<body> | |
<form action="" method="POST" id="frm"> | |
<fieldset> | |
<legend>Validar CPF</legend> | |
<label for="id_cpf">CPF</label> | |
<input type="text" name="cpf_field" id="id_cpf" /> | |
<input type="submit" value="validar" /> | |
</fieldset> | |
</form> | |
</body> | |
</html> |
This file contains 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
// anonymous original author, but here's a good source: http://www.tidbits.com.br/colecao-de-metodos-para-o-plugin-validate-do-jquery | |
(function ($) { | |
$.validator.addMethod('cpf',function(value,element,param) { | |
$return = true; | |
// this is mostly not needed | |
var invalidos = [ | |
'111.111.111-11', | |
'222.222.222-22', | |
'333.333.333-33', | |
'444.444.444-44', | |
'555.555.555-55', | |
'666.666.666-66', | |
'777.777.777-77', | |
'888.888.888-88', | |
'999.999.999-99', | |
'000.000.000-00' | |
]; | |
for(i=0;i<invalidos.length;i++) { | |
if( invalidos[i] == value) { | |
$return = false; | |
} | |
} | |
value = value.replace("-",""); | |
value = value.replace(/\./g,""); | |
//validando primeiro digito | |
add = 0; | |
for ( i=0; i < 9; i++ ) { | |
add += parseInt(value.charAt(i), 10) * (10-i); | |
} | |
rev = 11 - ( add % 11 ); | |
if( rev == 10 || rev == 11) { | |
rev = 0; | |
} | |
if( rev != parseInt(value.charAt(9), 10) ) { | |
$return = false; | |
} | |
//validando segundo digito | |
add = 0; | |
for ( i=0; i < 10; i++ ) { | |
add += parseInt(value.charAt(i), 10) * (11-i); | |
} | |
rev = 11 - ( add % 11 ); | |
if( rev == 10 || rev == 11) { | |
rev = 0; | |
} | |
if( rev != parseInt(value.charAt(10), 10) ) { | |
$return = false; | |
} | |
return $return; | |
}); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment