TextBox验证为空或为11位数字

本文关键字:11位 数字 验证 TextBox | 更新日期: 2023-09-27 17:59:41

我的表单中有一个文本框,只有当它为空或包含11位数字时,才必须对其进行验证。我尝试了互联网上找到的所有解决方案,但都没有成功。

<script type="text/javascript">
    $(document).ready(function() {
        $('#ctl00_CPH1_tax_id').attr("placeholder", "1234567890");
        $('#ctl00_CPH1_citizen_id').attr("placeholder", "12345678901");
        $('#ctl00_CPH1_e_posta').attr("placeholder", "ahmet.mehmet@sirket.com.tr");
        //var isbp = $('#ctl00_CPH1_IsBP');
        var name = $('#ctl00_CPH1_name').val();
        var code = $('#ctl00_CPH1_code').val();
        var dropdownlist2 = $('select:first');
        var tax_id = $('#ctl00_CPH1_tax_id').val();
        var citizen_id = $('#ctl00_CPH1_citizen_id').val();
        var email1 = $('#ctl00_CPH1_e_posta').val();
        var isbp = $('input[type=checkbox]:first');
        var dropdownlist1 = $('select:last');
        var isactive = $('input[type=checkbox]:last');
        //var dropdownlist1 = $('#ct100_CPH1_DropDownList1');
        var tax_id_check = /^'d{10}$/.test(tax_id);
        var citizen_id_check = /^$|^'d{11}$/.test(citizen_id);
        var email1_check = /^[a-zA-Z0-9._]+@([a-zA-Z0-9.]|[a-zA-Z0-9]+'-[a-zA-Z0-9.])+'.[a-zA-Z]{2,6}$/.test(email1);
        dropdownlist1.attr("disabled", "disabled");
        isactive.attr("disabled", "disabled");
        $(isbp).click(function(e) {
            if (isbp.is(':checked')) {
                dropdownlist1.removeAttr("disabled");
                isactive.removeAttr("disabled");
            }
            else {
                isactive.attr("disabled", "disabled");
                dropdownlist1.attr("disabled", "disabled");
            }
        });
        $("#ctl00_CPH1_Button1").click(function(e) {
            //console.log(drop2);
            //var name_status = /^[a-zA-Z][a-zA-Z]*$/.test(name);
            //var code_status = /^[a-zA-Z][a-zA-Z]*$/.test(code);
            //var IsBP = $('#ctl00_CPH1_IsBP');
            //if (name.length < 3 || name_status == false) {
            //    e.preventDefault();
            //    alert('İsim sadece karakterlerden oluşmalı ve en az iki karakter olmalı!');
            //}
            //if (code.length < 3 || code_status == false) {
            //    e.preventDefault();
            //    alert('Soyisim sadece karakterlerden oluşmalı ve en az iki karakter olmalı!');
            //}
            if (name.length == 0) {
                e.preventDefault();
                alert("İsim girilmedi!");
            }
            if (code.length == 0) {
                e.preventDefault();
                alert("Şirket Kodu girilmedi!");
            }
            if (dropdownlist2.val() == 0) {
                e.preventDefault();
                alert("Ülke seçilmedi!");
            }
            if (citizen_id.length != 0) {
                e.preventDefault();
                alert("Vatandaşlık numarası onbir haneli olmalı!");
            }
            if (tax_id.length == 0) {
                e.preventDefault();
                alert("Vergi numarası girilmedi!");
            }
            else if (!tax_id_check) {
                e.preventDefault();
                alert("Vergi numarası on haneli olmalı!");
            }
            if (email1 != "" && !email1_check) {
                e.preventDefault();
                alert("E-Posta geçerli değil!");
            }
            if (isbp.is(':checked') && dropdownlist1.val() == 0) {
                e.preventDefault();
                alert("İş ortağı tipi seçilmedi!");
            }
        });
    });

这是我的脚本,aspx如下:

    <form id="form1" runat="server">
<div>
<center>
    <table>
        <tr>
            <td colspan="3" align="center">
                Şirket Giriş Ekranı</td>
        </tr>
        <tr>
            <td colspan="3">&nbsp;</td>
        </tr>
        <tr>
            <td align="left">
                <asp:Label ID="Label1" runat="server" Text="İsim"></asp:Label>
            </td>
            <td colspan="2">
                <asp:TextBox ID="name" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td align="left">
                <asp:Label ID="Label2" runat="server" Text="Şirket Kodu"></asp:Label>
            </td>
            <td colspan="2">
                <asp:TextBox ID="code" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td align="left">
                <asp:Label ID="Label3" runat="server" Text="Ülke"></asp:Label>
            </td>
            <td class="style4" colspan="2">
                <asp:DropDownList ID="DropDownList2" runat="server" 
                    onselectedindexchanged="DropDownList2_SelectedIndexChanged">
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td align="left">
                <asp:Label ID="Label4" runat="server" Text="Vergi No"></asp:Label>
            </td>
            <td colspan="2">
                <asp:TextBox ID="tax_id" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td align="left">
                <asp:Label ID="Label5" runat="server" Text="Vergi Dairesi"></asp:Label>
            </td>
            <td colspan="2">
                <asp:TextBox ID="tax_office" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td align="left">
                <asp:Label ID="Label6" runat="server" Text="Vatandaşlık No"></asp:Label>
            </td>
            <td colspan="2">
                <asp:TextBox ID="citizen_id" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td align="left">
                <asp:Label ID="Label7" runat="server" Text="Adres"></asp:Label>
            </td>
            <td colspan="2">
                <asp:TextBox ID="address" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td align="left">
                <asp:Label ID="Label14" runat="server" Text="E-posta"></asp:Label>
            </td>
            <td colspan="2">
                <asp:TextBox ID="e_posta" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td align="left">
                <asp:Label ID="Label15" runat="server" Text="Telefon"></asp:Label>
            </td>
            <td colspan="2">
                <asp:TextBox ID="telefon" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td align="left">
                <asp:Label ID="Label16" runat="server" Text="Fax"></asp:Label>
            </td>
            <td colspan="2">
                <asp:TextBox ID="faks" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td align="left">
                <asp:Label ID="Label10" runat="server" Text="İş Ortağım"></asp:Label>
            </td>
            <td colspan="2">
                <asp:CheckBox ID="IsBP" runat="server" oncheckedchanged="IsBP_CheckedChanged"/> <!--AutoPostBack="true"--> 
                <asp:Label ID="Label11" runat="server" Text="Evet"></asp:Label>
            </td>
        </tr>
        <tr>
            <td align="left">
                <asp:Label ID="Label12" runat="server" Text="İş Ortağı Tipi"></asp:Label>
            </td>
            <td>
                <asp:DropDownList ID="DropDownList1" runat="server" 
                    onselectedindexchanged="DropDownList1_SelectedIndexChanged">
                </asp:DropDownList>
            </td>
            <td>
                <asp:CheckBox ID="IsActive" runat="server" 
                    oncheckedchanged="IsActive_CheckedChanged" />
                <asp:Label ID="Label13" runat="server" Text="Aktif"></asp:Label>
            </td>
        </tr>
        <tr>
            <td colspan="3">&nbsp;</td>
        </tr>
        <tr>
            <td>
                <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Geri" />
            </td>
            <td colspan="2" align="center">
                <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Kaydet" />
            </td>
        </tr>
    </table>
    <br />
</center>
</div>
</form>

我尝试了像1.8.3这样的jquery旧版本,它是稳定的,而不是1.9.1还有jquery.validate。当我在chrome上运行代码并在控制台上检查元素时,一切都能正常运行。但我的代码没有显示任何警报。"=="有效,但"!="或">"无效。提前感谢!无效的文本框是电子邮件和公民id。其他文本框工作正常。

TextBox验证为空或为11位数字

使用ASP。NET验证器,具有类似的正则表达式

"('d{11})?"
<asp:RegularExpressionValidator ControlToValidate="citizen_id" 
    ValidationExpression="('d{11})?" runat="server" 
    Text="Must be 11 digits or empty" />

如果文本框"citizen_id"不是"或11位数字,这将阻止回发。

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.regularexpressionvalidator.aspx