MVC 4日期格式化无法识别输入掩码(MVC 4 date formatting not recognizing input mask)
我遇到的问题是格式化为dd-MMM-yyyy的日期未被识别为日期。 我收到错误“字段开始日期必须是日期”。 对于使用datepicker 30-NOV-2013挑选的日期。
模型
[Required] [DataType(DataType.Date)] [Display(Name = "Start date")] [DisplayFormat(DataFormatString = "{0:dd-MMM-yyyy}", ApplyFormatInEditMode = true)] public DateTime StartDate { get; set; }查看@ Html.ValidationSummary()
@Html.LabelFor(m => m.StartDate) @Html.EditorFor(m => m.StartDate) @Html.ValidationMessageFor(m => m.StartDate) <div> <input type="submit" value="Set on leave" id="setLeave" /> </div> <script type="text/javascript"> $("#StartDate").datepicker({ showAnim: 'fadeIn', dateFormat: 'dd-M-yy', changeMonth: true, changeYear: true, showButtonPanel: true }).prop('readOnly', true); </script>I am having an issue where a date formatted as dd-MMM-yyyy is not being recognized as a date. I get the error "The field Start date must be a date." for a date picked with the datepicker 30-NOV-2013.
Model
[Required] [DataType(DataType.Date)] [Display(Name = "Start date")] [DisplayFormat(DataFormatString = "{0:dd-MMM-yyyy}", ApplyFormatInEditMode = true)] public DateTime StartDate { get; set; }View @Html.ValidationSummary()
@Html.LabelFor(m => m.StartDate) @Html.EditorFor(m => m.StartDate) @Html.ValidationMessageFor(m => m.StartDate) <div> <input type="submit" value="Set on leave" id="setLeave" /> </div> <script type="text/javascript"> $("#StartDate").datepicker({ showAnim: 'fadeIn', dateFormat: 'dd-M-yy', changeMonth: true, changeYear: true, showButtonPanel: true }).prop('readOnly', true); </script>最满意答案
我建议使用jQuery Globalize并自定义日期验证:
$(document).ready(function () { Globalize.culture('en-GB'); $.validator.methods.date = function (value, element) { return (this.optional(element) || Globalize.parseDate(value, 'dd-MMM-yyyy')); } });您可以在此处阅读有关jQuery Globalize的更多信息。
I would suggest to use jQuery Globalize and customize dates validation:
$(document).ready(function () { Globalize.culture('en-GB'); $.validator.methods.date = function (value, element) { return (this.optional(element) || Globalize.parseDate(value, 'dd-MMM-yyyy')); } });You can read something more about jQuery Globalize here.
更多推荐
发布评论