asp.net-mvc 在 Mvc 中验证日期格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16565532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Validate DateFormat In Mvc
提问by Abhishek
I have a property ExpiredDate define in MVC
我有一个属性 ExpiredDate 在 MVC 中定义
[Required]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime? ExpirationDate { get; set; }
I want to Validate if Date on a page is not in correct format.
The format of date I am using is MM/dd/yyyy.
我想验证页面上的日期格式是否不正确。我使用的日期格式是MM/dd/yyyy.
回答by truemedia
You should use the DataTypeattribute with DataType.Date. Both of which are in the System.ComponentModel.DataAnnotationsnamespace and can be used like this:
您应该将该DataType属性与DataType.Date. 两者都在System.ComponentModel.DataAnnotations命名空间中,可以这样使用:
[Required]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime? ExpirationDate { get; set; }
This answeralso includes some more attributes.
这个答案还包括一些更多的属性。
Update: To enable client-side validation in ASP.NET MVC4, you need to do this:
更新:要在 ASP.NET MVC4 中启用客户端验证,您需要执行以下操作:
Add the jquery.validation plugin to the footer
<%: Scripts.Render("~/Scripts/jquery.validate.min.js") %> <%: Scripts.Render("~/Scripts/jquery.validate.unobtrusive.min.js") %>Add this to web.config
<appSettings> <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> </appSettings>Use this css when using
@Html.ValidationMessageFor()so that it is initially hidden and displays via the javascript validation/* styles for validation helpers */ .field-validation-error { color: #e80c20; font-weight: bold; } .field-validation-valid { display: none; } input.input-validation-error { border: 1px solid #e80c20; } input[type="checkbox"].input-validation-error { border: 0 none; } .validation-summary-errors { color: #e80c20; font-weight: bold; font-size: 1.1em; } .validation-summary-valid { display: none; }
将 jquery.validation 插件添加到页脚
<%: Scripts.Render("~/Scripts/jquery.validate.min.js") %> <%: Scripts.Render("~/Scripts/jquery.validate.unobtrusive.min.js") %>将此添加到 web.config
<appSettings> <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> </appSettings>使用时使用此 css,
@Html.ValidationMessageFor()以便它最初隐藏并通过 javascript 验证显示/* styles for validation helpers */ .field-validation-error { color: #e80c20; font-weight: bold; } .field-validation-valid { display: none; } input.input-validation-error { border: 1px solid #e80c20; } input[type="checkbox"].input-validation-error { border: 0 none; } .validation-summary-errors { color: #e80c20; font-weight: bold; font-size: 1.1em; } .validation-summary-valid { display: none; }
回答by lukyer
Custom validation date format must be solved manually.
自定义验证日期格式必须手动解决。
Client validation issues can occur because of MVC bug (even in MVC 5) in jquery.validate.unobtrusive.min.jswhich does not accept date/datetime format in any way. It is not caused by datepicker nor browsers. Unfortunately you have to solve it manually.
客户端验证问题可能是由于jquery.validate.unobtrusive.min.js 中的 MVC 错误(即使在 MVC 5 中)而发生,它不以任何方式接受日期/日期时间格式。它不是由日期选择器或浏览器引起的。不幸的是,您必须手动解决它。
My finally working solution:
我的最终工作解决方案:
You have to include before:
您必须先包括:
@Scripts.Render("~/Scripts/jquery-3.1.1.js")
@Scripts.Render("~/Scripts/jquery.validate.min.js")
@Scripts.Render("~/Scripts/jquery.validate.unobtrusive.min.js")
@Scripts.Render("~/Scripts/moment.js")
You can install moment.js using:
您可以使用以下方法安装 moment.js:
Install-Package Moment.js
And then you can finally add fix for date format parser:
然后你终于可以为日期格式解析器添加修复:
$(function () {
$.validator.methods.date = function (value, element) {
return this.optional(element) || moment(value, "DD.MM.YYYY", true).isValid();
}
});
回答by Keren Caelen
You can use validation like:
您可以使用以下验证:
[Required]
[DisplayFormat(DataFormatString = "{0:d}")]
public DateTime? ExpirationDate { get; set; }
I think it will work
我认为它会起作用

