views:

2761

answers:

4

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 asp.net'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.

Thanks.

                        <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" />
                        </asp:CheckBoxList>
A: 

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
Next

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!

Richard.

Richard
Or a more compact form in C# would be: args.IsValid = ckBoxListReasons.Items.Any(item => item.Selected);
Jacob
You can't use .Any() because the Items collection is not descended from IEnumerable<T> - it's ListItemCollection.
Josh Kodroff
+4  A: 

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: www.asp.net 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

<asp:CheckBoxList ID="chkModuleList"runat="server" >
</asp:CheckBoxList>

<asp:CustomValidator runat="server" ID="cvmodulelist"
  ClientValidationFunction="ValidateModuleList"
  ErrorMessage="Please Select Atleast one Module" ></asp:CustomValidator>

// 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<chkListinputs .length;i++)
  {
    if (chkListinputs [i].checked)
    {
      args.IsValid = true;
      return;
    }
  }
  args.IsValid = false;
}

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.

Kelsey
+1  A: 

Check out this article from 4 Guys From Rolla:

http://aspnet.4guysfromrolla.com/articles/092006-1.aspx

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.

Austin
A: 

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;

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

Here's the markup:

<asp:CheckBoxList runat="server"
          Id="cblOptions" 
          DataTextField="Text" 
          DataValueField="Id" />

<xx:CustomValidator Display="Dynamic" 
              runat="server" 
              ID="cblOptionsValidator"
              ControlId="cblOptions"
              ClientValidationFunction="ValidateCheckBoxList" 
              ErrorMessage="One selection required." />

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);

        base.OnLoad(e);
    }
}
Nariman