同一文本框中的验证程序CPF e CNPJ(ASP.NET web应用程序)

Validator CPF e CNPJ in the same textbox (ASP.NET web application)

本文关键字:ASP CNPJ NET web 应用程序 CPF 文本 程序 验证      更新时间:2023-09-26

我有一个文本框,我想根据用户输入的数字数量验证这个字段。我解释一下:CPF的口罩是999.999.999-99,CNPJ的口罩是99.999.999/99999-99。我有这个代码,但它不起作用。我希望我的例子是这样的:https://jsfiddle.net/eL52cga5/

我的代码:

<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>

我搞定了!!!

<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>