I need to receive some time information in the format "hh:mm" (no seconds). The property is defined like this:
[DataType(DataType.Time), DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = @"{0:hh\:mm}")]
public TimeSpan SomeTimeProperty { get; set; }
The server side validation works as expected. However, I can't get the client side validation to work as no client-side validation rules are generated.
How can I make it work?
I am afraid you will need to go the long route and create a Custom validator attribute for it.
Then you need to write the TimeSpanValidationRule class:
This is enough to get the Html Helper generate a
data-val-timespan="Please specify a valid timespan (hh:mm)"
and adata-val-timespan-format="hh:mm"
for the html input box.This two values can be "harvested" by adding an adapter to javascript unobtrusive validation for the "timespan" attribute. It will then be validated by its corresponding rule (which will mimic the server side rule):