I have the following code:
[DisplayName("58.Date and hour of birth")]
[DataType(DataType.DateTime, ErrorMessage = "Please enter a valid date in the format dd/mm/yyyy hh:mm")]
[Range(typeof(DateTime), "1/1/2011", "1/1/2016")]
[RequiredToClose]
public object V_58 { get; set; }
I want to force the inclusion of time (in format hh:mm) and not only the date. This code considers 1/1/2011 as valid when it shouldn’t as it does not containt the hour , Any clue about how to express the correct format ? (dd/mm/yyyy hh:mm)
You could write your own
ValidationAttributeand decorate the property with it. You override theIsValidmethod with your own logic.And finally, decorate your property:
Note: Be wary of multiple validation attributes on your properties, as the order in which they are evaluated is unable to be determined without more customization, and subsequently if validation logic overlaps, your error messages might not accurately describe what exactly you mean to be wrong with the property (yeah, that’s a run-on sentence)