Home > ASP.Net, Javascript > Validate Number of Checked items in CheckListBox using Javascript

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();" />

Hope this helps

Good Luck

Advertisements
Categories: ASP.Net, Javascript
  1. Mehboob Ali
    May 31, 2013 at 5:40 pm

    Thanks nice one.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: