Greater Than or Equal To Today Date validation annotation in MVC3

前端 未结 3 1628
北恋
北恋 2020-12-05 02:53

Has anyone seen an MVC3 data annotation for Date validation that requires a single selected date to be equal to or greater than current date?

If there\'s already a t

3条回答
  •  孤城傲影
    2020-12-05 03:18

    Create a custom attribute.

    public class CheckDateRangeAttribute: ValidationAttribute {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext) {
            DateTime dt = (DateTime)value;
            if (dt >= DateTime.UtcNow) {
                return ValidationResult.Success;
            }
    
            return new ValidationResult(ErrorMessage ?? "Make sure your date is >= than today");
        }
    
    }
    

    code was written off the cuff so fix any errors :)

提交回复
热议问题