Pages

Monday, 10 October 2011

Validate Number of Checked items in CheckListBox using Javascript


Hi all ,
try this example to Validate Number of Checked items in CheckListBox using Javascript
try this Javascript function :-


<script language="javascript" type="text/javascript">
        var atLeast = 1
        function Validate() {
            var CHK = document.getElementById("<%=CheckBoxList1.ClientID%>");
            var checkbox = CHK.getElementsByTagName("input");
            var counter = 0;
            for (var i = 0; i < checkbox.length; i++) {

                if (checkbox[i].checked) {
                    counter++;
                 
                }
            }
            if (atLeast > counter) {
                alert("Please select atleast " + atLeast + " item(s)");
                return false;
            }
            return true;
        }
</script>

The HTML Page will be :-


<asp:CheckBoxList ID="CheckBoxList1" runat="server"  >
    </asp:CheckBoxList>
        <input id="Button1" type="button" value="button" onclick="javascript:Validate();" />


No comments:

Post a Comment