之前一定有人写过这个:-)
我需要一个出生日期的验证属性来检查日期是否在特定范围内 - 即用户没有输入尚未发生或过去 150 年的日期。
谢谢指点!
最佳答案
[DateOfBirth(MinAge = 0, MaxAge = 150)]
public DateTime DateOfBirth { get; set; }
// ...
public class DateOfBirthAttribute : ValidationAttribute
{
public int MinAge { get; set; }
public int MaxAge { get; set; }
public override bool IsValid(object value)
{
if (value == null)
return true;
var val = (DateTime)value;
if (val.AddYears(MinAge) > DateTime.Now)
return false;
return (val.AddYears(MaxAge) > DateTime.Now);
}
}
关于c# - 有人得到了 C# MVC 的出生日期验证属性吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3309689/