Skip to content

Instantly share code, notes, and snippets.

@fhferreira
Last active December 10, 2018 13:31
Show Gist options
  • Save fhferreira/6562409 to your computer and use it in GitHub Desktop.
Save fhferreira/6562409 to your computer and use it in GitHub Desktop.
Jquery Mask Nono Digito
// jQuery Masked Input
$('#celular').mask("(99) 9999-9999?9").ready(function(event) {
var target, phone, element;
target = (event.currentTarget) ? event.currentTarget : event.srcElement;
phone = target.value.replace(/\D/g, '');
element = $(target);
element.unmask();
if(phone.length > 10) {
element.mask("(99) 99999-999?9");
} else {
element.mask("(99) 9999-9999?9");
}
});
/*
Quem encontrar problemas com o "target", utilize esta alternativa
Enviado por: Irineu <irineujunior@gmail.com>
*/
$('#celular').focusout(function(){
var phone, element;
element = $(this);
element.unmask();
phone = element.val().replace(/\D/g, '');
if(phone.length > 10) {
element.mask("(99) 99999-999?9");
} else {
element.mask("(99) 9999-9999?9");
}
}).trigger('focusout');
//Em um projeto Legado usei o efeito blur.
$('#celular').blur(function(){
var phone, element;
element = $(this);
element.unmask();
phone = element.val().replace(/\D/g, '');
if(phone.length > 10) {
element.mask("(99) 99999-999?9");
} else {
element.mask("(99) 9999-9999?9");
}
element.unmask();
}).trigger('blur');
// jQuery Meio Mask
$('#celular').setMask("(99) 9999-99999").ready(function(event) {
var target, phone, element;
target = (event.currentTarget) ? event.currentTarget : event.srcElement;
phone = target.value.replace(/\D/g, '');
element = $(target);
element.unsetMask();
if(phone.length > 10) {
element.setMask("(99) 99999-9999");
} else {
element.setMask("(99) 9999-99999");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment