0

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.

2
  • You can use count to count the total checkboxes checked! I am not able to understand web-forms, but still I can give you a guess! :) Commented Oct 3, 2013 at 19:04
  • Four and only 4? Have you tried modifying it to suit your purposes? Why haven't you been able to get it to work? Did you consider using a CheckBoxList? Commented Oct 3, 2013 at 20:21

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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 )

Comments

0

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.

1 Comment

thank you for your answer, i tried this but i dont know which variable i have to change to get at least 4 checkboxes counted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.