0

我的表单中有一个文本框,只有当它为空或包含 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>

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

4

1 回答 1

2

使用带有正则表达式的 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

于 2013-02-15T04:30:57.030 回答