Asp.net mvc 2 使用DataTypeAttribute验证日期

Asp.net mvc 2 使用DataTypeAttribute验证日期,asp.net-mvc-2,Asp.net Mvc 2,我很难理解如何使用MVC2验证日期(DOB)。我想做的是1。输入的日期是否有效,2。是指过去至少13年的日期。例如,要验证电子邮件,我使用以下代码: [Required(ErrorMessage = "Email address is required.")] [StringLength(320, ErrorMessage = "Email must be less than 320 characters.")] [Email(ErrorMessage = "This email add

我很难理解如何使用MVC2验证日期(DOB)。我想做的是1。输入的日期是否有效,2。是指过去至少13年的日期。例如,要验证电子邮件,我使用以下代码:

[Required(ErrorMessage = "Email address is required.")]  
[StringLength(320, ErrorMessage = "Email must be less than 320 characters.")]  
[Email(ErrorMessage = "This email address is invalid.")]  
public string email { get; set; }  
要验证我使用的电子邮件,请执行以下操作:

public class EmailAttribute : RegularExpressionAttribute
{        
    public EmailAttribute()
        : base("insert long regex expression here") { }
}
任何帮助都将不胜感激,谢谢

试试这个:

public class YearsInThePast : RangeAttribute
{
    public YearsInThePast(int yearsInThePast) : base(
        typeof(DateTime), 
        DateTime.MinValue.ToString(), 
        DateTime.Now.AddYears(-yearsInThePast).ToString()
    )
    { }
}
还有你的模特:

public class MyModel
{
    [YearsInThePast(13, ErrorMessage = "Date must be 13 years in the past")]
    public DateTime Date { get; set; }
}