2
Good morning guys. I have an input where this is typed a value and as the size of this gets the mask CPF or CNPJ. The problem I’m having is that when a CPF is typed with the correct size for example, if the user presses the TAB key, ENTER or any other, the mask changes to that of CNPJ even not adding any number, that is, he is recognizing these keys as valid character and changes the mask. I was able to take the functionality of the tab key, for example, from not going to the next line, but it keeps changing the value mask. Is there any way I can take out all these keys or block the use of these in the input and just let the numbers interfere with the mask ?
HTML:
<div class="col-xs-5 col-sm-5 col-md-5 col-lg-3 text-center select_height" id="div_cnpj_fornecedor">
<b id="cnpj_cpf">CNPJ:</b>
<input name="cnpj" minlength="12" maxlength="20" class="font-pop" id="cnpj" placeholder="00.000.000/0000-00" required>
</div>
jQuery:
$(document).ready(function () {
$("#cnpj").keydown(function () {
try {
$("#cnpj").unmask();
} catch (e) { }
var tamanho = $("#cnpj").val().length;
if (tamanho < 11) {
$("#cnpj").mask("999.999.999-99");
$("#cnpj_cpf").text("CPF:");
} else {
$("#cnpj").mask("99.999.999/9999-99");
$("#cnpj_cpf").text("CNPJ:");
}
// ajustando foco
var elem = this;
setTimeout(function () {
// mudo a posição do seletor
elem.selectionStart = elem.selectionEnd = 10000;
}, 0);
// reaplico o valor para mudar o foco
var currentValue = $(this).val();
$(this).val('');
$(this).val(currentValue);
});
})
I am using this plugin: https://plugins.jquery.com/mask/
I appreciate any help provided.
tried to input type? text or number
– Diego