Click here to Skip to main content
15,921,279 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear Experts,

I have 4 dropdownlists and a submit button.

I want to validate it using javascript. during submit User must select at least one dropdownlist.

Please help.

Thanks

What I have tried:

 <script type="text/javascript">
    function Validate() {
        var Xddl1 = document.getElementById("<%=ddl1.ClientID %>");
         var Xddl2 = document.getElementById("<%=ddl2.ClientID %>");
          var Xddl3 = document.getElementById("<%=ddl3.ClientID %>");
           var Xddl4 = document.getElementById("<%=ddl4.ClientID %>");
       
        if(Xddl1r.getElementsByTagName == "" || Xddl2.getElementsByTagName =="" || Xddl3.getElementsByTagName == "" || Xddl4.getElementsByTagName == "")
        
        {
         
            alert("Please select atleast one!");
            return false;
        }
        return true;
    }
</script>
Posted
Updated 11-Jun-17 9:44am

1 solution

There are several way to do this. I'll assume, since you didn't post much information.
I assumed your application has dropdowns like below.
ASP.NET
//ddl1
//ddl2
<asp:DropDownList ID="ddl3" runat="server">
        <asp:ListItem Text="Select" Value=""></asp:ListItem>
        <asp:ListItem Text="Option 1" Value="1"></asp:ListItem>
        <asp:ListItem Text="Option 2" Value="2"></asp:ListItem>
        <asp:ListItem Text="Option 3" Value="3"></asp:ListItem>
        <asp:ListItem Text="Option 4" Value="4"></asp:ListItem>
    </asp:DropDownList>
...

Then the client side validation will look something like below
JavaScript
<script type="text/javascript">
        function Validate() {
            var Xddl1 = document.getElementById("<%=ddl1.ClientID %>");
            var Xddl2 = document.getElementById("<%=ddl2.ClientID %>");
            var Xddl3 = document.getElementById("<%=ddl3.ClientID %>");
            var Xddl4 = document.getElementById("<%=ddl4.ClientID %>");

            //answer[answer.selectedIndex].value
            if (Xddl1[Xddl1.selectedIndex].value == "" &&
                Xddl2[Xddl2.selectedIndex].value == "" &&
                Xddl3[Xddl3.selectedIndex].value == "" &&
                Xddl4[Xddl4.selectedIndex].value == "") {

                alert("Please select atleast one!");
                return false;
            }
            return true;
        }
    </script>


On second though, let keep it simple. Above code should work. The code below is per control, you might need to look into CustomValidator Control[^] to ensure at least one value from the dropdown list is being selected.
My prefer alternative is to use the Validation control which will handle the client and server side validation. Here is an example.
ASP.NET
//ddl1
...
<asp:DropDownList ID="ddl4" runat="server">
        <asp:ListItem Text="Select" Value="0"></asp:ListItem>
        <asp:ListItem Text="Option 1" Value="1"></asp:ListItem>
        <asp:ListItem Text="Option 2" Value="2"></asp:ListItem>
        <asp:ListItem Text="Option 3" Value="3"></asp:ListItem>
        <asp:ListItem Text="Option 4" Value="4"></asp:ListItem>
    </asp:DropDownList>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" 
         Display="Dynamic" ControlToValidate="ddl4" InitialValue="0"
        ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
    <asp:Button ID="Button1" runat="server" Text="Button" />

The above markup should handle the client side validation. Then on the server side, the code should call the Page.IsValid method to make sure whatever posted to the server are valid.
C#
if (Page.IsValid)
{
    // your code here.
}
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 12-Jun-17 0:17am    
5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900