我在使用DataAnnotations时遇到了问题,下面是我的代码:
[Display(Name = "Admission date")]
[DataType(DataType.Date, ErrorMessage = "The field {0} is not in the correct format")]
[Required(ErrorMessage = "The {0} field is required.")]
[Date(ErrorMessage = "Date for {0} must be between {1} and {2}")]
[RegularExpression(@"^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]|(?:Jan|Mar|May|Jul|Aug|Oct|Dec)))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2]|(?:Jan|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)(?:0?2|(?:Feb))\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9]|(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep))|(?:1[0-2]|(?:Oct|Nov|Dec)))\4(?:(?:1[6-9]|[2-9]\d)\d{2})$", ErrorMessage = "The field {0} is not in the correct format.")]
public DateTime AdmissionDate { get; set; }
它工作得很好(如果我在下面的代码中提到这一点):
[Display(Name = "Leadership date")]
[DataType(DataType.Date, ErrorMessage = "The field {0} is not in the correct format")]
[Date(ErrorMessage = "Date for {0} must be between {1} and {2}")]
[RegularExpression(@"^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]|(?:Jan|Mar|May|Jul|Aug|Oct|Dec)))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2]|(?:Jan|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)(?:0?2|(?:Feb))\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9]|(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep))|(?:1[0-2]|(?:Oct|Nov|Dec)))\4(?:(?:1[6-9]|[2-9]\d)\d{2})$", ErrorMessage = "The field {0} must be a date.")]
public Nullable<DateTime> HeadShipDate { get; set; }
我不知道为什么,但它确实发生了,我注释了HeadShipDate的正则表达式验证,并且AdmissionDate验证有效,如果我取消对HeadShipDate的验证的注释,它似乎很混乱,并且与任何日期都匹配。
顺便说一句,是相同的正则表达式。
发布于 2015-05-19 11:06:26
您的HeadShipDate正则表达式需要在一个字符串内,并以@符号开头,就像第一个一样。您发布的内容无法编译。
https://stackoverflow.com/questions/30315852
复制相似问题