1

我正在尝试使用 JQuery 在 ASP.net 中使用自定义验证器对多个字段进行验证,但我无法让它工作。

有一个包含两个值的下拉框,基于这些值出现其他项目。

因此,如果用户选择选项 1,则会出现一个文本框,如果他们选择选项 2,则文本框会消失并出现两个下拉框。

根据他们选择的选项,我想验证他们的输入。

到目前为止我没有工作的是

.ASPX

<asp:DropDownList ID="drpHeight" runat="server">
                            <asp:ListItem Value="CMs">CMs</asp:ListItem>
                            <asp:ListItem Value="Feet">Feet</asp:ListItem>
                        </asp:DropDownList>

<asp:TextBox ID="txtCm" runat="server" Width="100px"></asp:TextBox>
                         <asp:CustomValidator ID="CustomValidator9" runat="server" ErrorMessage="Please select you height" 
                    ClientValidationFunction = "ValidateHeight" Display="Dynamic" ControlToValidate="txtCm" ValidateEmptyText="true"></asp:CustomValidator> 
<asp:RegularExpressionValidator ID="revCm" runat="server" 
                    ControlToValidate="txtCm" Display="Dynamic" 
                    ErrorMessage="Please enter a valid number" 
                    ValidationExpression="^([0-9]*|\d*\.\d{1}?\d*)$" SetFocusOnError="True"></asp:RegularExpressionValidator>


<br />
<br />

<asp:DropDownList ID="drpFeet" runat="server">
                            <asp:ListItem Value="-1">Select...</asp:ListItem>
                            <asp:ListItem Value="1"></asp:ListItem>
                            <asp:ListItem Value="2"></asp:ListItem>
                            <asp:ListItem Value="3"></asp:ListItem>
                            <asp:ListItem Value="4"></asp:ListItem>
                            <asp:ListItem Value="5"></asp:ListItem>
                            <asp:ListItem Value="6"></asp:ListItem>
                            <asp:ListItem Value="7"></asp:ListItem>
                            <asp:ListItem Value="8"></asp:ListItem>
                        </asp:DropDownList>
                            <asp:Label ID="lblFeet" runat="server" Text="Ft"></asp:Label>
                        &nbsp;<asp:DropDownList ID="drpInches" runat="server">
                            <asp:ListItem Value="-1">Select...</asp:ListItem>
                            <asp:ListItem Value="0"></asp:ListItem>
                            <asp:ListItem Value="1"></asp:ListItem>
                            <asp:ListItem Value="2"></asp:ListItem>
                            <asp:ListItem Value="3"></asp:ListItem>
                            <asp:ListItem Value="4"></asp:ListItem>
                            <asp:ListItem Value="5"></asp:ListItem>
                            <asp:ListItem Value="6"></asp:ListItem>
                            <asp:ListItem Value="7"></asp:ListItem>
                            <asp:ListItem Value="8"></asp:ListItem>
                            <asp:ListItem Value="9"></asp:ListItem>
                            <asp:ListItem Value="10"></asp:ListItem>
                            <asp:ListItem Value="11"></asp:ListItem>
                        </asp:DropDownList>
                        &nbsp;<asp:Label ID="lblInches" runat="server" Text="Inches"></asp:Label> 
<br />
<br />

jQuery

        function ValidateHeight(sender, args) {

        if ($('#<%=drpHeight.ClientID%>').val = "CMs") {

            if ($('#<%=txtCm.ClientID%>').val().length > 0) {
                args.IsValid = true;

            }

            else {
                args.IsValid = false;
                return;
            }

        }

        else if ($('#<%=drpHeight.ClientID%>').val = "Feet") {
            args.IsValid = true;
        }
    }
        </script>

目前它不起作用,我确实让它验证了 CM b,但后来它停止了工作,现在我不知道为什么。

解决这个问题的最佳方法是什么?我还有另一个重量要做,一个有 3 种输入可能性。

4

1 回答 1

0

我终于找到了答案,我将为任何想知道如何做到这一点的人提供答案。

将我的客户验证器更改为:

 <asp:CustomValidator ID="CustomValidator9" runat="server" ErrorMessage="Please select you height" 
                    ClientValidationFunction = "ValidateHeight" Display="Dynamic" ValidateEmptyText="true"></asp:CustomValidator> 

然后我的 JQuery 变成了

<script type="text/javascript">

    function ValidateHeight(sender, args) {

        var drpHeight = $('#<%=drpHeight.ClientID%>').val();

        if (drpHeight == "CMs") {

            if ($('#<%=txtCm.ClientID%>').val().length > 0) {
                args.IsValid = true;
                return;

            }

            else {
                args.IsValid = false;
                return;
            }

        }

        else if (drpHeight == "Feet") {

            var drpFeet = $('#<%=drpFeet.ClientID%>').val();
            var drpInches = $('#<%=drpInches.ClientID%>').val();

            if (drpFeet == -1 || drpInches == -1) {
                args.IsValid = false;
                return;                    
            }
            else {
                args.IsValid = true;
                return;
            }
        }            

        }
</script>
于 2013-03-20T12:32:42.200 回答