Skip to content

Instantly share code, notes, and snippets.

@everaldomatias
Created May 18, 2018 11:57
Show Gist options
  • Save everaldomatias/dd6845ca280864344b4adcec98529f95 to your computer and use it in GitHub Desktop.
Save everaldomatias/dd6845ca280864344b4adcec98529f95 to your computer and use it in GitHub Desktop.
Máscara jQuery para CPF e CNPJ no mesmo campo
/*
Adiciona máscara em CPF e CNPJ no mesmo campo
Caso precise você pode mudar o seletor para usar um ID ou class.
Fonte: https://jsfiddle.net/pdd8g4mf/
*/
var CpfCnpjMaskBehavior = function (val) {
return val.replace(/\D/g, '').length <= 11 ? '000.000.000-009' : '00.000.000/0000-00';
},
cpfCnpjpOptions = {
onKeyPress: function(val, e, field, options) {
field.mask(CpfCnpjMaskBehavior.apply({}, arguments), options);
}
};
$(function() {
$(':input[name=cpfCnpj]').mask(CpfCnpjMaskBehavior, cpfCnpjpOptions);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment