0

I have 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.

View code
 ---------
 <% foreach (var item in Model)
             { %>  

                  <%:Html.CheckBox("EmployeId", new { value = item.EmployeeID,@class="required"})%>
                   <span class="field-validation-valid" id="Span1"></span>

                  <%:Html.LabelForModel(item.EmployeeName)%>
                  </div>
                 <%-- <%= Html.CheckBox("Accept", new { @class = "required" })%> 
                  <span class="field-validation-valid" id="Accept_validationMessage"></span>--%> 
          <%} %>                

    </fieldset> 
    <div class="assign_button">

Can anyone please give me some help with this?

1
  • That's because you haven't clicked the symbol on answers that you want to accepted. Commented Aug 9, 2012 at 7:32

2 Answers 2

1

Please call following method whereever you want to validate it. Change Checkbox class name and div name as per your implementation.

function ValidateCheckBoxCheckedOrNot()
    {
        var selectedCheckBoxesValue = '';

        $('#DIVID').find("input:checkbox.CheckBoxClassName:checked").each(function (i, selected) {
                                                            if (selectedCheckBoxesValue.length == 0) {
                                                                selectedCheckBoxesValue += $(selected).val();
                                                            }
                                                            else {
                                                                selectedCheckBoxesValue += ',' + $(selected).val();
                                                            }});
            // Here you also get all the comma separated values if you want else use below method for it
        if(selectedCheckBoxesValue.length == 0)
        {
            alert("Select atleast one checkbox");
        }
    }

or you can also do it using following way

function ValidateCheckBoxCheckedOrNot()
    {
        var selectedCheckBoxes = $('#DIVID').find("input:checkbox.CheckBoxClassName:checked").length;

        if(selectedCheckBoxes==0)
        {
            alert("Select atleast one checkbox");
        }
    }

EDITED POST

<div id="CheckBoxDiv">
<% foreach (var item in Model)
             { %>  

                  <%:Html.CheckBox("EmployeId", new { value = item.EmployeeID,@class="employeeCheckBox"})%>
                   <span class="field-validation-valid" id="Span1"></span>

                  <%:Html.LabelForModel(item.EmployeeName)%>
                  </div>
                 <%-- <%= Html.CheckBox("Accept", new { @class = "required" })%> 
                  <span class="field-validation-valid" id="Accept_validationMessage"></span>--%> 
          <%} %> 
</div>   

<script type="text/javascript">
    //Call following method on your button click event.
    function ValidateCheckBoxCheckedOrNot()
            {
                var selectedCheckBoxes = $('#CheckBoxDiv').find("input:checkbox.employeeCheckBox:checked").length;

                if(selectedCheckBoxes==0)
                {
                    alert("Select atleast one checkbox");
                }
            }
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

@RaghavendraBandaru .. If its work for u than pls mark it as an answer.
1

Am I right that you want client validation? If you use jQuery, assert that this collection

  $('input[type="checkbox"].required:checked');

greater than zero.

I mean something like that:

function validateCheckbox(){
    var count = 0;
    $('input[type="checkbox"].required:checked').each(function() { ++count; } );
    return count > 0;
}

You have button <div class="assign_button">. In this context you can write:

$(function(){
    $('div.assign_button').click(function(){
            if (!validateCheckbox()) {
                alert('Please, check something!');
            }
            else {
                alert('You are wonderful!');
            }
        });
});

2 Comments

@Kipwoker...please give me deatil in jquery also
function validateCheckbox(){ var count = 0; $('input[type="checkbox"].required:checked').each(function() { ++count; } ); return count > 0; }

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.