how to validate a user chose at least one checkbox

how to validate a user chose at least one checkbox  using -',.net,validation,checkbox,checkboxlist'

I've got a checkbox group control that I want to require the user to check at least ONE box, it does not matter if they check every single one, or 3, or even just one.

In the spirit of's validation controls can I use to enforce this. I'm also using the Ajax validation extender, so it would be nice if it could look like other controls, and not some cheesy server validate method in the codebehind.

<asp:CheckBoxList RepeatDirection="Horizontal" RepeatLayout="Table" RepeatColumns="3" ID="ckBoxListReasons" runat="server">
    <asp:ListItem Text="Preliminary Construction" Value="prelim_construction" />
    <asp:ListItem Text="Final Construction" Value="final_construction" />
    <asp:ListItem Text="Construction Alteration" Value="construction_alteration" />
    <asp:ListItem Text="Remodel" Value="remodel" />
    <asp:ListItem Text="Color" Value="color" />
    <asp:ListItem Text="Brick" Value="brick" />
    <asp:ListItem Text="Exterior Lighting" Value="exterior_lighting" />
    <asp:ListItem Text="Deck/Patio/Flatwork" Value="deck_patio_flatwork" />
    <asp:ListItem Text="Fence/Screening" Value="fence_screening" />
    <asp:ListItem Text="Landscape - Front" Value="landscape_front" />
    <asp:ListItem Text="Landscape - Side/Rear" Value="landscape_side_rear" />
    <asp:ListItem Text="Other" Value="other" />


asked Oct 19, 2015 by dahiyabecomp
0 votes

5 Answers

0 votes

It's easy to do this validation server side, but I am assuming you want to do it client side?

JQuery can do this very easily as long as you have something that all checkbox controls have in common to use as a selector such as class (CssClass on your .NET control). You can make a simple JQuery function and connect it to a ASP.NET custom validator. Remember if you do go the custom validator route to make sure you check it server side as well in case javascript is not working, you don't get a free server side check like the other .NET validators.

For more information on custom validators check out the following links: and MSDN

You don't need to use JQuery, it just makes the javascript function to iterate and look at all your checkbox controls much easier but you can just use vanilla javascript if you like.

Here is an example I found at: Link to original

// javascript to add to your aspx page
function ValidateModuleList(source, args)
  var chkListModules= document.getElementById ('<%= chkModuleList.ClientID %>');
  var chkListinputs = chkListModules.getElementsByTagName("input");
  for (var i=0;i

Side Note: JQuery is just a little js file include you need to add to your page. Once you have it included you can use all the JQuery you like. Nothing to install and it will be full supported in the next version of Visual Studio I think.

answered Oct 19, 2015 by mcasudhir
0 votes

Here's a cleaner jQuery implementation that allows one ClientValidationFunction for any number of CheckBoxList controls on a page:

function ValidateCheckBoxList(sender, args) {
    args.IsValid = false;

    $("#" +"table[id$="+sender.ControlId+"]").find(":checkbox").each(function () {
        if ($(this).attr("checked")) {
        args.IsValid = true;

Here's the markup:

And finally, the custom validator that allows the client function to retrieve the target control by ID:

public class CustomValidator : System.Web.UI.WebControls.CustomValidator
    public string ControlId { get; set; }

    protected override void OnLoad(EventArgs e)
        if (Enabled)
            Page.ClientScript.RegisterExpandoAttribute(ClientID, "ControlId", ControlId);

answered Oct 19, 2015 by manju bhargava
0 votes

Check out this article from 4 Guys From Rolla:

They show you how to create a validator for the CheckBox and CheckBoxList controls that works exactly like the RequiredFieldValidator for the other .NET controls. It has the server side validation and the client side validation. The nice part about that is you can specify a ValidationGroup and it will work with the ValidationSummary control.

There is also a link at the bottom of the article to download the source and use it in your project. You just reference the dll and register the controls and you are good to go.

answered Oct 19, 2015 by deepak07.s
0 votes

Here is another solution that may be considered via Dado.Validators on GitHub.


Example codebehind.aspx.cs

btnSubmit.Click += (a, b) =>
    if (Page.IsValid) {
        // Validation Successful

Ref: Check if a checkbox is checked in a group of checkboxes in clientside

answered Oct 19, 2015 by kotmus2002
0 votes

Loop through each of the items in ckBoxListReasons. Each item will be of type 'ListItem'.

The ListItem will have a property called 'Selected' that is a boolean. It's true when that item is selected. Something like:

Dim bolSelectionMade As Boolean = False
For Each item As ListItem in ckBoxListReasons.Items
 If item.Selected = True Then
  bolSelectionMade = True
 End If

bolSelectionMade will be set to true if the user has made at least one selection. You can then use that to set the Valid state of any particular validator control you like.

Hope this helps!


answered Oct 19, 2015 by sameer rathore