加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-15 23:52:15 所属栏目:asp.Net 来源:网络整理
导读:我在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])[/
我在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对象.正则表达式总是会错过闰年等细微之处.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读