MVC 模型验证日期 [英] MVC model validation for date
问题描述
MVC 5 是否有任何默认验证,我可以在其中设置日期的最小值和最大值?
Is there any default validation for MVC 5 where I can set min and max value of date?
在我的模型中我想要日期验证
In my model i want date validation
public class MyClass
{
[Required(ErrorMessage="Start date and time cannot be empty")]
//validate:Must be greater than current date
[DataType(DataType.DateTime)]
public DateTime StartDateTime { get; set; }
[Required(ErrorMessage="End date and time cannot be empty")]
//validate:must be greater than StartDate
[DataType(DataType.DateTime)]
public DateTime EndDateTime { get; set; }
}
Ps:根据这个 Asp.Net 网站,有一个将 Range
验证器用于 DateTime 的问题,不推荐使用.
Ps: According to this Asp.Net Website, there is a problem in using the Range
validator for DateTime, and it is not recommended.
注意:jQuery 验证不适用于 Range 属性和约会时间.例如,以下代码将始终显示一个客户端验证错误,即使日期在指定范围:
Note: jQuery validation does not work with the Range attribute and DateTime. For example, the following code will always display a client side validation error, even when the date is in the specified range:
[Range(typeof(DateTime), "1/1/1966", "1/1/2020")]
您需要禁用 jQuery 日期验证才能将 Range 属性与 DateTime 一起使用.硬编译通常不是一个好习惯模型中的日期,因此使用 Range 属性和 DateTime 是气馁.
You will need to disable jQuery date validation to use the Range attribute with DateTime. It's generally not a good practice to compile hard dates in your models, so using the Range attribute and DateTime is discouraged.
我也知道有 Nuget 包,例如 Fluent Validation 和 Foolproof 可以验证和检查一个日期是否大于另一个日期,但我想知道默认情况下是否有东西可以检查日期的最小值和最大值.
I also, know that there are Nuget packages like Fluent Validation and Foolproof that does the trick for validating and checking if one date is greater than other, but I wanted to know if by default there is something to check the date's min and max value.
根据我在 MVC 5.1 中的新功能 中看到的,有支持 MaxLength
和 MinLength
验证.
From what I saw in Whats new in MVC 5.1, There is support for MaxLength
and MinLength
validation.
推荐答案
无需禁用 jQuery 日期验证(这可能会导致其他问题).您只需要覆盖 $.validator
的 range
方法.
There is no need to disable jQuery date validation (and that is likely to cause other issues). You just need to override the range
method of the $.validator
.
默认情况下,它处理数值(然后回退到字符串比较),因此您可以添加以下脚本(在 jquery.validate.js
和 jquery.validate 之后.unobtrusive.js
,但没有包含在 $(document).ready
By default, it works with numeric values (and then falls back to a string comparison), so you can add the following script (after jquery.validate.js
and jquery.validate.unobtrusive.js
, but not wrapped in $(document).ready
$.validator.methods.range = function(value, element, param) {
if ($(element).attr('data-val-date')) {
var min = $(element).attr('data-val-range-min');
var max = $(element).attr('data-val-range-max');
var date = new Date(value).getTime();
var minDate = new Date(min).getTime();
var maxDate = new Date(max).getTime();
return this.optional(element) || (date >= minDate && date <= maxDate);
}
// use the default method
return this.optional( element ) || ( value >= param[ 0 ] && value <= param[ 1 ] );
};
然后你可以在你的属性上使用RangeAttribute
Then you can use the RangeAttribute
on your property
[Range(typeof(DateTime), "1/1/1966", "1/1/2020")]
public DateTime Date { get; set; }
这篇关于MVC 模型验证日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!