I recommend using the plugin jQuery Masked input, he’s better than the mask plugin
.
Example of the use of masked input
working with a similar CPF and CNPJ scheme in a single field:
jQuery(function($){
$('.cpf-cnpj').change(function(){
var campo = $(this).val();
if (campo == "cpf"){
$("#label-cpf-cnpj").html('CPF');
$("#InputCpf-cnpj").mask("999.999.999-99");
}
else if (campo == "cnpj"){
$("#label-cpf-cnpj").html('CNPJ');
$("#InputCpf-cnpj").mask("99.999.999/9999-99");
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery.maskedinput/1.4.1/jquery.maskedinput.min.js"></script>
<input type="radio" class="cpf-cnpj" id="cpf-cnpj" name="cpf/cnpj" value="cpf"> CPF<br>
<input type="radio" class="cpf-cnpj" id="cpf-cnpj" name="cpf/cnpj" value="cnpj"> CNPJ<br>
<br>
<label id="label-cpf-cnpj">CPF/CNPJ</label> <input type="text" id="InputCpf-cnpj" name="cpf/cnpj">
I did some tests here copying and pasting Cpfs and/ or Cnpjs complete with the plugin in question and works normally.
Example of the use of mask plugin
to visualize the difference between the two:
jQuery(function($){
$('.cpf-cnpj').change(function(){
var campo = $(this).val();
if (campo == "cpf"){
$("#label-cpf-cnpj").html('CPF');
$("#InputCpf-cnpj").mask("999.999.999-99");
}
else if (campo == "cnpj"){
$("#label-cpf-cnpj").html('CNPJ');
$("#InputCpf-cnpj").mask("99.999.999/9999-99");
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.0/jquery.mask.js"></script>
<input type="radio" class="cpf-cnpj" id="cpf-cnpj" name="cpf/cnpj" value="cpf"> CPF<br>
<input type="radio" class="cpf-cnpj" id="cpf-cnpj" name="cpf/cnpj" value="cnpj"> CNPJ<br>
<br>
<label id="label-cpf-cnpj">CPF/CNPJ</label> <input type="text" id="InputCpf-cnpj" name="cpf/cnpj">
Note that using the masked input
, when the user clicks on the field the entire mask is already displayed facilitating for the same the display of how many digits should be inserted in the field, different from the mask plugin
, where the mask will only appear gradually as the user enters the digits of the CPF or CNPJ.
Edit: Then follow a new example without using radio
to select CPF or CNPJ:
$(document).ready(function () {
$("#InputCpf-cnpj").mask("999.999.999-99?99999");
$('#InputCpf-cnpj').keyup(function (e) {
var query = $(this).val().replace(/[^a-zA-Z 0-9]+/g,'');
if (query.length == 11) {
$("#label-cpf-cnpj").html('CPF');
$("#InputCpf-cnpj").mask("999.999.999-99?99999");
}
if (query.length == 14) {
$("#label-cpf-cnpj").html('CNPJ');
$("#InputCpf-cnpj").mask("99.999.999/9999-99");
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery.maskedinput/1.4.1/jquery.maskedinput.min.js"></script>
<br>
<label id="label-cpf-cnpj">CPF/CNPJ</label><input id="InputCpf-cnpj" type="text">
the sitema in whom the mask is inserted was made all with Mask-plugin and is already in homologation, today it is not worth the exchange but will be the code you sent.
– Marcelo Souza
@Marcelosouza But it doesn’t change much, just change the import of
mask plugin
for the import ofmasked input
, note that the two codes I posted above are identical, changing only the import of the plugin used.– Leandro
look what you posted was very good however, at the request of the analyst should not have checkbox to choose which, he has to go typing and as you type the mask go changing, so I end up going back to the code I posted at the beginning, the Mask-plugin loses using two masks
– Marcelo Souza
@Marcelosouza I edited the post with a 3rd new example without using the
input radio
to choose between CPF or CNPJ. Make sure you now meet what you need.– Leandro