asp.net-mvc – 在mvc中以MM / dd / YYYY格式验证日期

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – 在mvc中以MM / dd / YYYY格式验证日期前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在MVC信息文件中声明了一个属性
[required(ErrorMessage = "End Date has not being entered")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}",ApplyFormatInEditMode = true)]
        [RegularExpression(@"^(0[1-9]|1[012])[/](0[1-9]|[12][0-9]|3[01])[/]\d{4}$",ErrorMessage = "End Date should be in MM/dd/yyyy format")]
        public DateTime? ExpirationDate { get; set; }

但是当我以正确的格式输入日期时,如2013年5月13日.它仍然显示错误消息

End Date should be in MM/dd/yyyy format

我遗漏了什么代码,或者上面有任何其他错误.

解决方法

您无法使用正则表达式验证日期,请使用DateTime.TryParseExact将字符串转换为DateTime对象.正则表达式总是会错过闰年等细微之处.
原文链接:https://www.f2er.com/aspnet/250976.html

猜你在找的asp.Net相关文章