micheljuca 24 de mai. de 2006
usa javascript pra mascara.
jaboot 24 de mai. de 2006
ae mano, usa isso no teu evento onBlur
e passa o objeto, não passa só o valor beleza?
ex.
< input type = "text" name = "CPF" onBlur = "javascript:validaCPF(this);" >
function validaCPF ( cpf)
{
erro = new String ;
if ( cpf . value . length == 11 )
{
cpf . value = cpf . value . replace ( '.' , '' );
cpf . value = cpf . value . replace ( '.' , '' );
cpf . value = cpf . value . replace ( '-' , '' );
var nonNumbers = /\ D / ;
if ( nonNumbers . test ( cpf . value ))
{
erro = "A verificacao de CPF suporta apenas números!" ;
}
else
{
if ( cpf . value == "[telefone removido]" ||
cpf . value == "[telefone removido]" ||
cpf . value == "[telefone removido]" ||
cpf . value == "[telefone removido]" ||
cpf . value == "[telefone removido]" ||
cpf . value == "[telefone removido]" ||
cpf . value == "[telefone removido]" ||
cpf . value == "[telefone removido]" ||
cpf . value == "[telefone removido]" ||
cpf . value == "[telefone removido]" ) {
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 . value . 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 . value . charAt ( 9 ) != a [ 9 ]) || ( cpf . value . charAt ( 10 ) != a [ 10 ])) {
erro = "Número de CPF inválido." ;
}
}
}
else
{
if ( cpf . value . length == 0 )
return false
else
erro = "Número de CPF inválido." ;
}
if ( erro . length > 0 ) {
alert ( erro );
cpf . focus ();
return false ;
}
return true ;
}
// envento onkeyup
function maskCPF ( CPF) {
var evt = window . event ;
kcode = evt . keyCode ;
if ( kcode == 8 ) return ;
if ( CPF . value . length == 3 ) { CPF . value = CPF . value + '.' ; }
if ( CPF . value . length == 7 ) { CPF . value = CPF . value + '.' ; }
if ( CPF . value . length == 11 ) { CPF . value = CPF . value + '-' ; }
}
// evento onBlur
function formataCPF ( CPF)
{
with ( CPF )
{
value = value . substr ( 0 , 3 ) + '.' +
value . substr ( 3 , 3 ) + '.' +
value . substr ( 6 , 3 ) + '-' +
value . substr ( 9 , 2 );
}
}
function retiraFormatacao ( CPF)
{
with ( CPF )
{
value = value . replace ( "." , "" );
value = value . replace ( "." , "" );
value = value . replace ( "-" , "" );
value = value . replace ( "/" , "" );
}
}
micheljuca 24 de mai. de 2006
pronto, tai a faca e o queijo na mao.
So se preocupa em jogar isso num arquivo de funcoes javascript e nao no codigo da jsp
mila.systems 7 de mar. de 2008
Putz, mto bom esse código!!!
valeu mesmo!!! :lol:
ilanocf 10 de jul. de 2008
Olá pessoal!
Sou novato aqui e como hoje em dia precisamos muito de javascript para criarmos nossas páginas web, espero aqui acumular bastante informação e conhecimento com todos.
Olá balarini,
Excelente esse script! Mas percebi q ele tb pode trabalhar com máscara, como posso implementar essa função usando a máscara? Uso Visual Studio 2005.
Grato,
Ilano.
Metaleiro 21 de out. de 2010
[color=darkblue]Sei que o tópico é antigo, mas esse código de validação de CPF está muito bom [/color]