我在使用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的Regex验证,并且AdmissionDate验证有效,如果我取消对HeadShipDate的验证,它似乎很困惑并且匹配任何日期。
顺便说一下,是正则表达式。
答案 0 :(得分:0)
HeadShipDate的正则表达式需要在字符串中,并以@符号开头,就像第一个一样。你发布的内容不会编译。