user1224129
user1224129

Reputation: 2779

DateTime Validation - force to enter time

In my C#/MVC4/Razor Application I need to make sure that user enter not only date, but also time. My Model is:

[Required]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy HH:mm}", ApplyFormatInEditMode = true)]
public DateTime Start { get; set; }

If user enters 02/03/2012, it will be converted to 02/03/2012 00:00.

I tried to implement custom ValidationAttribute, but I receive the value only after it is already converted to DateTime and I don't know, if user entered only date or date with time ("00:00" value).

Is it possible to force user enter date and time?

Upvotes: 1

Views: 686

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039548

You could write a custom model binder for the DateTime type which will take the DisplayFormat attribute format into account when binding the value. Here's an example such binder.

Upvotes: 1

Related Questions