Dotnet ReSource
Dotnet ReSource

Reputation: 201

Checkbox validation in mvc3?

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?

Upvotes: 0

Views: 797

Answers (2)

alok_dida
alok_dida

Reputation: 1733

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>

Upvotes: 1

kipwoker
kipwoker

Reputation: 196

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!');
            }
        });
});

Upvotes: 1

Related Questions