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.

Revisions

  1. flpinheiro revised this gist Aug 5, 2022. No changes.
  2. flpinheiro created this gist Aug 5, 2022.
    22 changes: 22 additions & 0 deletions DateLimitValidationAttribute.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    [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;
    }
    }
    }