I have created a datepicker using MVC5 DataType.Date:
[DataType(DataType.Date)]
public DateTime ArrivalDate { get; set; }
How can I set the minimum available date to Today and disable all past dates?
Alternatively, is there an option to disable a custom range of dates?
I have tried to create a custom DateRangeValidator using the this answer, but with that I get the following error: named parameter type constraints
Here's my Date range validator:
public DateTime FirstDate = DateTime.Today.Date;
//public DateTime SecondDate { get; set; }
protected override ValidationResult IsValid(DateTime date, ValidationContext validationContext)
{
// your validation logic
if (date >= DbFunctions.TruncateTime(FirstDate))
{
return ValidationResult.Success;
}
else
{
return new ValidationResult("Date is not valid.");
}
}
Try building a custom ValidationAttribute
with your own logic :
public sealed class PresentOrFutureDateAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
// your validation logic
if (Convert.ToDateTime(value) >= DateTime.Today)
{
return ValidationResult.Success;
}
else
{
return new ValidationResult("Past date not allowed.");
}
}
}