Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC model validation for date

Tags:

Is there any default validation for MVC 5 where I can set min and max value of date?

In my model i want date validation

    public class MyClass     {                        [Required(ErrorMessage="Start date and time cannot be empty")]         //validate:Must be greater than current date         [DataType(DataType.DateTime)]         public DateTime StartDateTime { get; set; }          [Required(ErrorMessage="End date and time cannot be empty")]         //validate:must be greater than StartDate         [DataType(DataType.DateTime)]         public DateTime EndDateTime { get; set; }                 } 

Ps: According to this Asp.Net Website, there is a problem in using the Range validator for DateTime, and it is not recommended.

Note: jQuery validation does not work with the Range attribute and DateTime. For example, the following code will always display a client side validation error, even when the date is in the specified range:

[Range(typeof(DateTime), "1/1/1966", "1/1/2020")]

You will need to disable jQuery date validation to use the Range attribute with DateTime. It's generally not a good practice to compile hard dates in your models, so using the Range attribute and DateTime is discouraged.

I also, know that there are Nuget packages like Fluent Validation and Foolproof that does the trick for validating and checking if one date is greater than other, but I wanted to know if by default there is something to check the date's min and max value.

From what I saw in Whats new in MVC 5.1, There is support for MaxLength and MinLength validation.