0
I have a script that masks CPF or CNPJ, is working perfectly. but it uses the method onKeyPress
, then it only works, when I’m typing. when I bring an object that is CNPJ
to show, the field CPF/CNPJ gets like this: 000.000.000-00000
. when behind CPF becomes normal, with the mask: 000.000.000-00
The question, is how do I get the CNPJ, with the correct mask when I’m bringing an object?.
My script is this:
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.0/jquery.mask.js"></script>
<script>
//Mascara CPF ou CNPJ
var options = {
onKeyPress: function (cpf, ev, el, op) {
var masks = ['000.000.000-000', '00.000.000/0000-00'];
$('#cnpj_cpf').mask((cpf.length > 14) ? masks[1] : masks[0], op);
}
}
$('#cnpj_cpf').length > 11 ? $('#cnpj_cpf').mask('00.000.000/0000-00', options) : $('#cnpj_cpf').mask('000.000.000-00#', options);
</script>
The mask I want to show is like this: 00.000.000/0000-00
worked perfectly. thank you!
– Rafael Passos