I'm trying to make a mask for CPF and CNPJ within the same field, the user can choose whether to fill the CPF or CNPJ through two RadioButtons, but the problem is that I can not change the mask of the field when alternating between a option or another. I wanted that when the user clicked on CPf the mask assumed the format of cpf and the same when clicking on cnpj. I'm doing the page using ASP.net and also the Jquery-Mask-Plugin. I do not know why, but the mask () function is not working in my code, so I chose to use the data-mask attribute in the input itself. Here is my code:
$(document).ready(function () {
var $txtCpf = $("#<%=txtCPF.ClientID %>");
if ($("#<%=selectModeInsc.ClientID %> input").val() == "CPF"){
$txtCpf.attr("data-mask", "999.999.999-99");
}
$("#<%=selectModeInsc.ClientID %> input").click(function () {
if (valor == "CNPJ") {
$txtCpf.removeAttr("data-mask");
$txtCpf.attr("data-mask", "99.999.999/0001-99");
}
});
});
<div class="col-lg-3">
<div class="form-group">
<asp:RadioButtonList
ID="selectModeInsc" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Text="CPF" Value="CPF" Selected="True"/>
<asp:ListItem Text="CNPJ" Value="CNPJ"/>
</asp:RadioButtonList>
<asp:TextBox runat="server" ID="txtCPF" class="form-control" MaxLength="14" ></asp:TextBox>
</div>
</div>
Note: I have tried in several ways, this code above is one of the times I implemented.