I just made my own annotation in C # and matched it with my jQuery validation. Now I just comment on any flag where this happens. If you are not using C #, you can simply just add the class to the element you want to apply to.
[System.AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)] public class CheckboxRequired : ValidationAttribute, IClientValidatable { protected override ValidationResult IsValid(object value, ValidationContext validationContext) { if (value.GetType() != typeof(bool) || (bool)value == true) return ValidationResult.Success; return new ValidationResult("This checkbox must be checked."); } public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) { var rule = new ModelClientValidationRule { ErrorMessage = "This checkbox must be checked.", ValidationType = "CheckboxRequired" }; yield return rule; } }
And in my Validation.js
jQuery.validator.addMethod("CheckboxRequired", function (value, element) { return (value != typeof undefined && value != false);}); jQuery.validator.addClassRules("CheckboxRequired", { CheckboxRequired: true});
Benjamin grebner
source share