Wednesday, May 15, 2019

ASP.NET MVC - Required Checkbox with Data Annotations


https://dotnetfiddle.net/JbPh0X
[Range(typeof(bool), "true", "true", ErrorMessage = "This field is required")]
public bool IsDiscforAffirmation { get; set; }

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);
        }
    }
 
 
 @Html.CheckBoxFor(k => k.IsChecked, new { @id = "IsChecked" })
<label for="IsChecked" style="font-weight: normal;">Agree</label><br />
@Html.ValidationMessageFor(k=>k.IsChecked) 

No comments:

Post a Comment

javascript Filter/index off

 var family = [{"name":"Jack",  "age": 26},               {"name":"Jill",  "age"...