我想正确验证日期。如果我输入日期为21-2-198而不是21-2-1988 。日期在Mvc3中没有正确验证。我使用了Regex但没有用。还有其他任何方法来验证日期,包括MVC模型中的年份。以下是我在模型中编写的代码
[Required(ErrorMessage = "Activation date is required")]
[Display(Name = "Activation date")]
[RegularExpression(@"/^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d+$/", ErrorMessage = "Enter proper date")]
public DateTime? ActivationDate { get; set; }
答案 0 :(得分:0)
您应该能够使用[DataType(DataType.Date)]
属性而不是正则表达式进行验证。请注意,此批注与区域设置无关......即21-2-1988
和2-21-1998
都将验证。
答案 1 :(得分:0)
我以 21-2-198 而不是21-2-1988输入日期。日期未在Mvc3中正确验证。
在我看来,您需要一个CustomValidator来阻止用户输入特定范围之外的日期。
ASP.NET MVC 3 Custom Validation using Data Annotations为此目的提供了一个验证器。