Skip to content

Instantly share code, notes, and snippets.

@HenriqueSilverio
Last active December 18, 2015 03:59
Show Gist options
  • Save HenriqueSilverio/5721927 to your computer and use it in GitHub Desktop.
Save HenriqueSilverio/5721927 to your computer and use it in GitHub Desktop.
Máscara para inputs que aceita o número 9 extra quando necessário.
/*
Masked input phone
Dependências:
- jQuery: http://jquery.com/
- Masked input: http://digitalbush.com/projects/masked-input-plugin/
*/
$(function() {
$('#fone').mask("(99) 9999-9999?9").keydown(function() {
var $elem = $(this);
var tamanhoAnterior = this.value.replace(/\D/g, '').length;
setTimeout(function() {
var novoTamanho = $elem.val().replace(/\D/g, '').length;
if (novoTamanho !== tamanhoAnterior) {
if (novoTamanho === 11) {
$elem.unmask();
$elem.mask("(99) 99999-9999");
} else if (novoTamanho === 10) {
$elem.unmask();
$elem.mask("(99) 9999-9999?9");
}
}
}, 1);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment