首先,我使用此解决方案解决了我的问题:jQuery date validation MVC 4 .Net 4.5.1 UK date "must be a date" Error
这是模型中的日期字段:
[DataType(DataType.DateTime)]
[DisplayFormat(DataFormatString = "{0:dd.MM.yyyy HH:mm}")]
[Display(Name = "Date")]
public DateTime Date{ get; set; }
以下是create.cshtml中的代码
<div class="form-group">
@Html.LabelFor(model => model.Date, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.Date, "{0:dd.MM.yyyy HH:mm}", new { @Value = ViewBag.Date, @class = "form-control", @id = "datetimepicker" })
@Html.ValidationMessageFor(model => model.Date, "", new { @class = "text-danger" })
</div>
</div>
layout.cshtml中的Javasript:
<script type="text/javascript">
jQuery('#datetimepicker').datetimepicker({
format: 'd.m.Y H:i',
lang: 'cs',
dayOfWeekStart: 1
});
这里是jQuery日期验证的重做:
date: function (value, element) {
$.culture = Globalize.culture("cs-CZ");
var date = Globalize.parseDate(value, "dd.MM.yyyy HH:mm","cs-CZ");
return this.optional(element) ||
!/Invalid|NaN/.test(new Date(date).toString());
},
编辑:所以我最终通过将日期函数更改为这样的正则表达式来实现它的工作:
return value.match(/^(0?[1-9]|[12][0-9]|3[0-1])[/., -](0?[1-9]|1[0-2])[/., -](19|20)?\d{2} ([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/);