Skip to content

Instantly share code, notes, and snippets.

@rhuanbarreto
Created January 14, 2017 13:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rhuanbarreto/4e221541a0927170743f22a2ef258600 to your computer and use it in GitHub Desktop.
Save rhuanbarreto/4e221541a0927170743f22a2ef258600 to your computer and use it in GitHub Desktop.
Máscaras comuns pt-br para jQuery MaskedInput
jQuery(function($) {
$('#cnpj,.cnpj').mask("99.999.999/9999-99");
$('#cep,.cep').mask("99.999-999");
$('#telefone-fixo,.telefone-fixo').mask("(99) 9999-9999");
$('#telefone-celular,.telefone-celular').mask("(99) 99999-9999");
// Se for um campo input do tipo date, mostrar máscara somente no Safari e Firefox pra evitar problemas com validação e mobile
var isFirefox = typeof InstallTrigger !== 'undefined';
var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0 || (function(p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window['safari'] || safari.pushNotification);
if (isFirefox || isSafari) {
$('#prazo,.prazo').mask("99/99/9999");
}
//Máscara de telefone fixo ou celular automático
$.mask.definitions['~'] = ['+-'];
$('#telefone-celular-fixo,.telefone-celular-fixo').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');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment