Skip to content

Instantly share code, notes, and snippets.

@flpinheiro
Created August 5, 2022 18:05
Show Gist options
  • Save flpinheiro/de28c53d103ca00c24393746a4b9fbb1 to your computer and use it in GitHub Desktop.
Save flpinheiro/de28c53d103ca00c24393746a4b9fbb1 to your computer and use it in GitHub Desktop.
[AttributeUsage(AttributeTargets.Property)]
public class DateLimitValidationAttribute : RequiredAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
base.IsValid(value, validationContext);
if (value == null) return ValidationResult.Success;
var date = Convert.ToDateTime(value);
var ageOfDateInDays = DateTime.Today.Subtract(date).Days;
if (ageOfDateInDays > 90)
{
return new ValidationResult("Select Date can not be older than 90 days, or 3 months.");
}
else
{
return ValidationResult.Success;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment