Here is my version based on the previous answer (but working both on the client side and on the server):
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)] public class RangeYearToCurrent : RangeAttribute,IClientValidatable { public RangeYearToCurrent(int from) : base(from, DateTime.Today.Year) { } #region IClientValidatable Members public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) { var rules = new ModelClientValidationRangeRule(this.ErrorMessage, this.Minimum, this.Maximum); yield return rules; } #endregion }
Usage example:
[RangeYearToCurrent(1995, ErrorMessage = "Invalid Date")] public int? Year { get; set; }
source share