I need to validate 2 dates: "begin date" and "end date". "Begin date" must from today to the future and "end date" must be equal or greater than "Begin date". How can I do it with Data Annotations? Is it possible?
Asked
Active
Viewed 1.8k times
1
-
Have you looked into custom validation attributes? – Ant P Mar 02 '15 at 11:31
-
Take a look at this http://www.dotnetglobe.com/2012_01_01_archive.html – Mahesh Mar 02 '15 at 11:32
-
http://stackoverflow.com/questions/10887824/greater-than-or-equal-to-today-date-validation-annotation-in-mvc3, http://stackoverflow.com/questions/18577777/mvc-validation-lower-higher-than-other-value, http://stackoverflow.com/questions/19882296/mvc4-data-annotation-compare-two-dates, http://stackoverflow.com/questions/7025198/mvc-custom-validation-compare-two-dates – CodeCaster Mar 02 '15 at 11:34
-
I have this custom validation attribute: public class MyDate1Attribute : ValidationAttribute { public override bool IsValid(object value) { DateTime d = Convert.ToDateTime(value); return d >= DateTime.Today; } } but I get this error: Error 1 'Dominio.elm' does not implement interface member 'System.ComponentModel.DataAnnotations.IValidatableObject.Validate(System.ComponentModel.DataAnnotations.ValidationContext)' C:\Users\baptista_l1\Documents\visual studio 2012\Projects\ELM2\Dominio\elm.cs – Luís Baptista Mar 02 '15 at 11:35
1 Answers
3
You can do this with DataAnnotations
as mentioned in bellow links:
- Greater Than or Equal To Today Date validation annotation in MVC3
- MVC Validation Lower/Higher than other value
- mvc4 data annotation compare two dates
- MVC custom validation: compare two dates
But, as an advice, solve this via Javascript libraries that are more easy than DataAnnotation
s like Jquery Input Mask

Community
- 1
- 1

Amirhossein Mehrvarzi
- 18,024
- 7
- 45
- 70
-
2Client-side Validation (like Jquery input Mask) is great, but always check server-side too .. ( http://stackoverflow.com/a/15855799/130420 ) – Guillaume Dec 20 '15 at 08:27