mvc validate date/time is at least 1 minute in the future

asp.net-mvc, c#, validation

Solution

Create a new Attribute:

public class FutureDateAttribute : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        return value != null && (DateTime)value > DateTime.Now;
    }
}

Now in your model set this attribute:

[FutureDate(ErrorMessage="Date should be in the future.")]
public DateTime Deadline { get; set; }

Problem

I am still getting my hands around MVC. I have seen several similar questions, some custom code and various methods but I have not found something that works for me. I have a search model that fills an HTML table with results inside of a partial view. I have this in my search results model: ``` public DateTime? BeginDateTime { get; set; } ``` Which is set to DateTime.Now in the controller. The user can specify that date and time to run a task with the search results' data on the model's POST call. What I would like to do is validate that the date/time the user defined is at least 1 minute in the future. If this can be done as a client-side validation it will be better, but I am open to options as long as it works. View: ``` Begin update: @Html.TextBoxFor(o => o.BeginDateTime, new { id="txtBegin" }) ``` Thanks.

Original source