Last active
March 28, 2019 03:14
-
-
Save leonidaswander/7f6dd26863f8cf9577d7fb998447e487 to your computer and use it in GitHub Desktop.
Diversas Mascaras com ER JavaScript
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
//Basta atribuir a classe respectiva no input | |
$('.telefone').on('keyup', function() { | |
mascara(this, mascara_tel); | |
$(this).attr('maxlength','15'); | |
}); | |
$('.cpf').on('keyup', function() { | |
mascara(this, mascara_cpf); | |
$(this).attr('maxlength','14'); | |
}); | |
$('.cep').on('keyup', function() { | |
mascara(this, mascara_cep); | |
$(this).attr('maxlength','9'); | |
}); | |
$('.moeda').on('keyup', function() { | |
mascara(this, mascara_moeda); | |
$(this).attr('maxlength','14'); | |
}); | |
$('.numero').on('keyup', function() { | |
mascara(this, mascara_num); | |
}); | |
$('.cnpj').on('keyup', function() { | |
mascara(this, mascara_cnpj); | |
$(this).attr('maxlength','18'); | |
}); | |
$('.data').on('keyup', function() { | |
mascara(this, mascara_data); | |
$(this).attr('maxlength','10'); | |
}); | |
$('.cartao_credito').on('keyup', function() { | |
mascara(this, mascara_cartaocredito); | |
$(this).attr('maxlength','19'); | |
}); | |
function mascara(o,f){ | |
v_obj=o | |
v_fun=f | |
setTimeout("execmascara()",1) | |
} | |
function execmascara(){ | |
v_obj.value=v_fun(v_obj.value) | |
} | |
function mascara_cep(v){ | |
v=v.replace(/\D/g,"") | |
v=v.replace(/^(\d{5})(\d)/,"$1-$2") | |
return v | |
} | |
function mascara_tel(v){ | |
v=v.replace(/\D/g,""); | |
v=v.replace(/^(\d{2})(\d)/g,"($1) $2"); | |
v=v.replace(/(\d)(\d{4})$/,"$1-$2"); | |
return v; | |
} | |
function mascara_cnpj(v){ | |
v=v.replace(/\D/g,"") | |
v=v.replace(/^(\d{2})(\d)/,"$1.$2") | |
v=v.replace(/^(\d{2})\.(\d{3})(\d)/,"$1.$2.$3") | |
v=v.replace(/\.(\d{3})(\d)/,".$1/$2") | |
v=v.replace(/(\d{4})(\d)/,"$1-$2") | |
return v | |
} | |
function mascara_cpf(v){ | |
v=v.replace(/\D/g,"") | |
v=v.replace(/(\d{3})(\d)/,"$1.$2") | |
v=v.replace(/(\d{3})(\d)/,"$1.$2") | |
v=v.replace(/(\d{3})(\d{1,2})$/,"$1-$2") | |
return v | |
} | |
function mascara_data(v){ | |
v=v.replace(/\D/g,""); | |
v=v.replace(/(\d{2})(\d)/,"$1/$2"); | |
v=v.replace(/(\d{2})(\d)/,"$1/$2"); | |
v=v.replace(/(\d{2})(\d{2})$/,"$1$2"); | |
return v; | |
} | |
function mascara_num(v){ | |
v=v.replace(/\D/g,""); | |
return v; | |
} | |
function mascara_moeda(v){ | |
v=v.replace(/\D/g,""); | |
v=v.replace(/(\d)(\d{8})$/,"$1.$2"); | |
v=v.replace(/(\d)(\d{5})$/,"$1.$2"); | |
v=v.replace(/(\d)(\d{2})$/,"$1,$2"); | |
return v; | |
} | |
function mascara_cartaocredito(v){ | |
v=v.replace(/\D/g,""); | |
v=v.replace(/^(\d{4})(\d)/g,"$1 $2"); | |
v=v.replace(/^(\d{4})\s(\d{4})(\d)/g,"$1 $2 $3"); | |
v=v.replace(/^(\d{4})\s(\d{4})\s(\d{4})(\d)/g,"$1 $2 $3 $4"); | |
return v; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment