Question

I am trying to do validation on multiple fields with the custom validator in ASP.net using JQuery but I can't get it to work.

There is a drop down box which contains two values, based on these values other items appear.

So if the user selects option 1, then a text box appears, if they select option 2 the text box disappears and two drop down boxes appear.

Depending on what option they choose I want to validate their input.

What I have so far that is not working is

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

At the moment it is not working, I did have it validating the CM b but then it stopped working and now I can't figure out why.

What is the best way to approach this? I've got another one for Weight to do as well, that one has 3 input possibilities.

Was it helpful?

Solution

I came across the answer finally, I will provide the answer for anyone else who wants to know how to do this.

Changed my customer validator to:

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

Then my JQuery became

<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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top