2016-05-15 11 views
0

我有一个文本框,我想根据用户输入的数字数量验证此字段。我解释一下:CPF的掩码是999.999.999-99,CNPJ的掩码是99.999.999/9999-99。我有这个代码,但它不起作用。 我希望我的例子是这样的: https://jsfiddle.net/eL52cga5/Validator CPF e CNPJ在同一个文本框中(ASP.NET web应用程序)

我的代码:

<script src="https://code.jquery.com/jquery-2.1.4.js"></script> 
    <script src="Scripts/jquery.maskedinput.js" type="text/javascript"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      $("#txtCPF").keydown(function() { 
       try { 
        $("#txtCPF").unmask(); 
       } catch (e) { } 

       var tamanho = $("#txtCPF").val().length; 

       if (tamanho < 11) { 
        $("#txtCPF").mask("999.999.999-99"); 
       } else if (tamanho >= 11) { 
        $("#txtCPF").mask("99.999.999/9999-99"); 
       } 
      }); 
     }); 
    </script> 

<asp:TextBox ID="txtCPF" name="txtCPF" runat="server" Width="150px" EnableViewState="false" placeholder="CPF ou CNPJ*"></asp:TextBox>

回答

0

我得到了它!

<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script> 
    <script src="https://igorescobar.github.io/jQuery-Mask-Plugin/js/jquery.mask.min.js"></script> 
    <script src="https://fiddle.jshell.net/js/lib/dummy.js"></script> 
    <script type='text/javascript'>//<![CDATA[ 
     window.onload=function(){ 
      $("#txtCPF").keydown(function(){ 
       try { 
        $("#txtCPF").unmask(); 
       } catch (e) {} 

       var tamanho = $("#txtCPF").val().length; 

       if(tamanho < 11){ 
        $("#txtCPF").mask("999.999.999-99"); 
       } else if(tamanho >= 11){ 
        $("#txtCPF").mask("99.999.999/9999-99"); 
       }     
      }); 
     }//]]> 
</script> 
相关问题