Validate Multiple Select DropDownList using JavaScript on single Button Click

Last Reply on Dec 26, 2012 12:16 AM By RkHirpara

Posted on Dec 25, 2012 10:57 PM

 there more than 1 dropdown controls used

Posted on Dec 26, 2012 12:16 AM

Hi Meenakhi,

Please try the following approach.

 

<head runat="server">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    <script type="text/javascript">
        $("[id*=Submit]").live("click", function () {
            var flag = true;
            $(".ddl").each(function () {
                if ($(this)[0].selectedIndex <= 0) {
                    flag = false;
                }
            });
            if (!flag) {
                alert("Any of the ddl can not be unselected.");
            }
            return flag;
        });
    </script>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server" CssClass="ddl">
            <asp:ListItem Text="Please Select" Value="0" Selected="True"></asp:ListItem>
            <asp:ListItem Text="Asp Forums" Value="1"></asp:ListItem>
            <asp:ListItem Text="Asp Snippets" Value="2"></asp:ListItem>
            <asp:ListItem Text="jQueryFAQS" Value="3"></asp:ListItem>
        </asp:DropDownList>
        <asp:DropDownList ID="DropDownList2" runat="server" CssClass="ddl">
            <asp:ListItem Text="Please Select" Value="0" Selected="True"></asp:ListItem>
            <asp:ListItem Text="Apple" Value="1"></asp:ListItem>
            <asp:ListItem Text="Mango" Value="2"></asp:ListItem>
            <asp:ListItem Text="Banana" Value="3"></asp:ListItem>
        </asp:DropDownList>
        <asp:Button ID="Submit" Text="Ok" runat="server" />
        <br />  
    </div>
    </form>
</body>

 

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html