Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have tree check boxes like below names

First Counter
Second Counter
Third Counter


so that i can choose more than one check box ,but i need to validate user should select atleast one checkbox

This is My Model

C#
class AddDetails
{
 public Boolean FirstCounter{ get; set; }
 public Boolean SecondCounter{ get; set; }
 public Boolean ThirdCounter{ get; set; }
  
}

This is my view
VB
@Html.CheckBoxFor(mmm => mmm.FirstCounter) <label>First</label>
@Html.CheckBoxFor(mmm => mmm.SecondCounter) <label>Second</label>
@Html.CheckBoxFor(mmm => mmm.ThirdCounter) <label>Third</label>

am using @Html.validatemessagefor() for error message purpose

so if the user not selected any one of the above check boxes, alert message should shown to user using validation message
Posted
Updated 7-Jul-14 1:13am
v2
Comments
jo.him1988 8-Jul-14 9:46am    
hi why did you not use javascript for this
try this simple script

1 solution

XML
<script type="text/javascript" src="../../Scripts/jquery-1.7.1.min.js">

</script>
<script type="text/javascript">
    function CheckBox() {

        if ($("#chk1").get(0).checked || $("#chk2").get(0).checked || $("#chk3").get(0).checked)
            return true;
        else {
            alert("check atleast one checkbox");
            return false;
        }
    }
</script>





@Html.CheckBox("firstChkBox",new {id="chk1"})<label>First</label>

@Html.CheckBox("secondChkBox", new { id = "chk2" })<label>Second</label>
@Html.CheckBox("thirdChkBox",new {id="chk3"}) <label>Third</label>
<input type="button" value="checkBox" id="btnChk" onclick="return CheckBox();"/>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900