我正在尝试为我的 mvc 应用程序创建简单的正则表达式验证。我试过:
[Required]
[RegularExpression("(\\d\\d\\/\\d\\d\\/\\d\\d\\d\\d)")]
而且它不工作。我想强制客户以 01/01/2015 格式输入日期
最佳答案
为什么在这种情况下需要正则表达式?使用 DisplayFormat
和 DataType
:
[Required]
[DataType(DataType.DateTime)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
我的意思是用正则表达式验证日期是错误的。没有简单的方法来验证诸如 31/02/2015
或 31/04/2015
之类的无效日期。
关于c# - 简单的正则表达式日期验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34059054/