1
votes

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?

1
Have you looked into custom validation attributes?Ant P
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.csLuís Baptista

1 Answers

3
votes

You can do this with DataAnnotations as mentioned in bellow links:

  1. Greater Than or Equal To Today Date validation annotation in MVC3
  2. MVC Validation Lower/Higher than other value
  3. mvc4 data annotation compare two dates
  4. MVC custom validation: compare two dates

But, as an advice, solve this via Javascript libraries that are more easy than DataAnnotations like Jquery Input Mask