Tuesday, October 6, 2015

Validate Listbox Ckeckbox Abd Radio Buttons

<asp:ListBox ID="ListBox1" runat="server" Rows="6" SelectionMode="Multiple">
            <asp:listitem text="India" value="1"></asp:listitem>
            <asp:listitem text="US" value="2"></asp:listitem>
            <asp:listitem text="UK" value="3"></asp:listitem>
            <asp:listitem text="China" value="4"></asp:listitem>
            <asp:listitem text="Russia" value="5"></asp:listitem>
            <asp:listitem text="New Zealand" value="6"></asp:listitem>
        </asp:ListBox>
        <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Required two country" ClientValidationFunction="Validate_ListBox"></asp:CustomValidator>
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" /><asp:ListBox ID="ListBox1" runat="server" Rows="6" SelectionMode="Multiple">
            <asp:listitem text="India" value="1"></asp:listitem>
            <asp:listitem text="US" value="2"></asp:listitem>
            <asp:listitem text="UK" value="3"></asp:listitem>
            <asp:listitem text="China" value="4"></asp:listitem>
            <asp:listitem text="Russia" value="5"></asp:listitem>
            <asp:listitem text="New Zealand" value="6"></asp:listitem>
        </asp:ListBox>
        <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Required two country" ClientValidationFunction="Validate_ListBox"></asp:CustomValidator>
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
        <script>
            function Validate_ListBox(sender, args) {
                var Listoptions = document.getElementById("<%=ListBox1.ClientID%>").options;
                var Selectedcount = 0;
                for (var i = 0; i < Listoptions.length; i++) {
                    if (Listoptions[i].selected == true) {
                        Selectedcount++;
                    }
                }
                if (Selectedcount <= 2) {
                    args.IsValid = false;
                } else {
                    args.IsValid = true;
                }
            }
        </script>


        /////////////////////////////////////////////
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <script>
            function CheckBoxList1_Validation(sender, args) {
                args.IsValid = false;
                var cnt = $("#CheckBoxList1 input:checked").length;
                args.IsValid = (cnt >= 2);
            }
        </script>
        Select at least 2 countries:
        <asp:CheckBoxList ID="CheckBoxList1" runat="server">
            <asp:listitem text="India" value="1"></asp:listitem>
            <asp:listitem text="US" value="2"></asp:listitem>
            <asp:listitem text="UK" value="3"></asp:listitem>
            <asp:listitem text="China" value="4"></asp:listitem>
            <asp:listitem text="Russia" value="5"></asp:listitem>
            <asp:listitem text="New Zealand" value="6"></asp:listitem>
        </asp:CheckBoxList>
        <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Please select at least 2 countries" ClientValidationFunction="CheckBoxList1_Validation" ForeColor="Red"></asp:CustomValidator>
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" />


        ///////////////////////////////////////////////////////
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <script>
            function RadioButtonList1_Validate(sender, args) {
                args.IsValid = ($("#RadioButtonList1 :radio:checked").length > 0);
            }
        </script>
        <asp:RadioButtonList ID="RadioButtonList1" runat="server">
           <asp:listitem text="India" value="1"></asp:listitem>
            <asp:listitem text="US" value="2"></asp:listitem>
            <asp:listitem text="UK" value="3"></asp:listitem>
            <asp:listitem text="China" value="4"></asp:listitem>
            <asp:listitem text="Russia" value="5"></asp:listitem>
            <asp:listitem text="New Zealand" value="6"></asp:listitem>
        </asp:RadioButtonList>
        <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Please select a country" Display="Dynamic" ClientValidationFunction="RadioButtonList1_Validate" ForeColor="Red"></asp:CustomValidator>
        <br />

        <asp:Button ID="btnSubmit" runat="server" Text="Submit" />