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对象.正则表达式总是会错过闰年等细微之处.

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....