@model MvcRequiredCheckbox.SampleViewModel
<title>ASP.NET MVC - Required Checkbox with Data Annotations</title>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
.field-validation-error {
<div class="col-md-6 col-md-offset-3">
<h1>ASP.NET MVC - Required Checkbox with Data Annotations</h1>
@using (Html.BeginForm())
@Html.CheckBoxFor(x => x.TermsAndConditions)
@Html.LabelFor(x => x.TermsAndConditions)
@Html.ValidationMessageFor(x => x.TermsAndConditions)
<button type="submit" class="btn btn-success submit">Submit</button>
<div class="credits text-center">
<a href="http://jasonwatmore.com/post/2013/10/16/ASPNET-MVC-Required-Checkbox-with-Data-Annotations.aspx">ASP.NET MVC - Required Checkbox with Data Annotations</a>
<a href="http://jasonwatmore.com">JasonWatmore.com</a>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/jquery.validate.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/mvc/5.2.3/jquery.validate.unobtrusive.js"></script>
// extend jquery range validator to work for required checkboxes
var defaultRangeValidator = $.validator.methods.range;
$.validator.methods.range = function(value, element, param) {
if(element.type === 'checkbox') {
// if it's a checkbox return true if it is checked
// otherwise run the default validation function
return defaultRangeValidator.call(this, value, element, param);