-3
Hello, I looked for this doubt in the questions, but I could not find it. Well, I would like to make a field to have the masks of CPF and CNPJ and CEI. depending on the number of characters typed. Here it doesn’t work totally well, because with several tests I did here, this my code didn’t work that I need. I don’t know if I should just change, or if I need to make another.
The format of the ERC is: 00.000.00000/00
I tried this code, but without any success:
$(".cpf_cnpj_cei").mask("000.000.000-00", {
onKeyPress: function(cpfcnpj, e, field, options) {
const masks = ["000.000.000-00", "00.000.00000/00", "00.000.000/0000-00"];
let mask = null;
if (cpfcnpj.length < 14) {
mask = masks[0];
} else if (cpfcnpj.length > 14 && cpfcnpj.length < 18) {
mask = masks[1];
} else if (cpfcnpj.length > 18) {
mask = masks[2];
}
$(".cpf_cnpj_cei").mask(mask, options);
}
});
<input class="cpf_cnpj_cei" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.15/jquery.mask.min.js"></script>
REFERENCES
Mask for CPF and CNPJ in the same field. Mask for CPF and CNPJ in the same field
CIS generator: https://theonegenerator.com/pt/geradores/documentos/gerador-de-cei
jQuery Mask Plugin
A plugin to make masks on form Fields.
https://igorescobar.github.io/jQuery-Mask-Plugin/
I took into account in the closing as duplicate the fact that the answer is practically the same that had already been posted on the given link, taking away the adjustments. To understand what kind of question serves the site and, consequently, avoid closures and negativities worth reading What is the Stack Overflow and the Stack Overflow Survival Guide (summarized) in Portuguese. Customization for very specific cases does not eliminate the feature of duplication (and not quite the purpose of the site).
– Bacco