user2157063
user2157063

Reputation: 49

asp.net c# 4 checkbox validation

I'm just sitting in front of my first real asp.net c# project and I dont know how to validate that the user must checked 4 checkboxes to get to the next step on my site.

ASP.Net

<div class="panel-body">
<p>Select 4 Items from the List</p>
 <label class="checkbox">
    <asp:CustomValidator ID="SelectValidator" runat="server"
        ErrorMessage="Please select FOUR (4) Items!"
        OnServerValidate="SelectValidator_ServerValidate"></asp:CustomValidator>
    <asp:CheckBox ID="CheckBox1" Text="" runat="server" /><br>
    <asp:CheckBox ID="CheckBox2" Text="" runat="server" /><br>
    <asp:CheckBox ID="CheckBox3" Text="" runat="server" /><br>
    <asp:CheckBox ID="CheckBox4" Text="" runat="server" /><br>
    <asp:CheckBox ID="CheckBox5" Text=" " runat="server" /><br>
    <asp:CheckBox ID="CheckBox6" Text="" runat="server" /><br>
    <asp:CheckBox ID="CheckBox7" Text="" runat="server" /><br>
    <asp:CheckBox ID="CheckBox8" Text="" runat="server" /><br>
    <asp:CheckBox ID="CheckBox9" Text="" runat="server" /><br>
</label>
</div>

Code Behind

 protected void SelectValidator_ServerValidate(object source, ServerValidateEventArgs args)
 {
     args.IsValid = CheckBox1.Checked;
 }

That is what I have and found in the internet right now for Server and Clientsite.

Upvotes: 0

Views: 3097

Answers (3)

borkovski
borkovski

Reputation: 978

Here's something that should suit your problem:

http://www.aspsnippets.com/Articles/Validate-ASPNet-CheckBoxList-at-least-one-CheckBox-checked-using-Custom-Validator.aspx

I'm sure changing this snippet to suit your needs will be pretty straightforward.

Edit: The part you should change to test for exactly 4 checked boxes goes like this:

<script type="text/javascript">
    function ValidateCheckBoxList(sender, args) {
        var checkBoxList = document.getElementById("<%=chkFruits.ClientID %>");
        var checkboxes = checkBoxList.getElementsByTagName("input");
    var checkboxCounter = 0;
        for (var i = 0; i < checkboxes.length; i++) {
            if (checkboxes[i].checked) {
                checkboxCounter++;
            }
        }
        args.IsValid = (checkboxCounter == 4);
    }
</script>

The only thing left to change is some fruit references here and there and the content of checkBoxList itself.

Upvotes: 0

Christian Phillips
Christian Phillips

Reputation: 18749

You could use JQuery, and disable/enable the button based on the count...

$("input:checkbox:checked").length

If the length is >= 4, then enable the button. This will prevent the postbacks, but then you could validate the count on the button click to make sure 4 are actually checked, this giving only one postback.

Upvotes: 1

Moho
Moho

Reputation: 16498

Try using a CheckBoxList control (MSDN). Then, in the code-behind, try the following:

// count how many checkboxes are checked
var checkedItemCount = chkBoxList.Items.Count( li => li.Selected )

Upvotes: 0

Related Questions