Вы можете написать пользовательский механизм связывания для проверки на стороне сервера:
public class DateModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if (value == null)
{
return null;
}
var format = bindingContext.ModelMetadata.EditFormatString ?? string.Empty;
format = format.Replace("{0:", string.Empty).Replace("}", string.Empty);
if (!string.IsNullOrEmpty(format))
{
DateTime date;
if (DateTime.TryParseExact(value.AttemptedValue, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
{
return date;
}
}
return base.BindModel(controllerContext, bindingContext);
}
}
, который вы зарегистрируете в Application_Start
:
ModelBinders.Binders.Add(typeof(DateTime?), new DateModelBinder());
Для проверки на стороне клиента естьразличные методы, которые могут быть использованы.Например, вы можете обработать это вручную:
<script>
$.validator.addMethod('myDate', function (value, element) {
// TODO: validate if the value corresponds to the required format
// and return true or false based on it
return false;
}, 'Please enter a date in the format yyMMdd');
$(function () {
// Attach the myDate custom rule to the #Something element
$('#Something').rules('add', 'myDate');
});
</script>