I have a checkbox validation before i submit the page. But it works in reverse. It's displaying error message when i check the box instead of opposite. I don't know where im doing wrong.
My ViewModel
[Display(Name = "Terms and Conditions")]
[Range(typeof(bool), "true", "true", ErrorMessage = "Please accept Terms & Conditions")]
public bool IsTermsAccepted { get; set; }
My View
<div class="row col-lg-offset-2 top-buffer">
@Html.CheckBoxFor(model => model.IsTermsAccepted)
@Html.LabelFor(model => model.IsTermsAccepted)
<br>@Html.ValidationMessageFor(model => model.IsTermsAccepted)
</div>
Thank you for your time!
Edit1: I followed exactly like here
Edit2: I was able to resolve this by adding a simple script (as shown in the above mentioned link)
<script>
// extend range validator method to treat checkboxes differently
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
return element.checked;
} else {
// otherwise run the default validation function
return defaultRangeValidator.call(this, value, element, param);
}
}