You can use a RelativeDateTimeValidator
to validate an age based on a Birth Date. For example:
public class Person
{
[RelativeDateTimeValidator(-150, DateTimeUnit.Year, RangeBoundaryType.Inclusive,
0, DateTimeUnit.Year, RangeBoundaryType.Ignore,
MessageTemplate="Person must be less than 150 years old.")]
public DateTime BirthDate
{
get;
set;
}
}
// 150 Year old person
Person p = new Person() { BirthDate = DateTime.Now.AddYears(-150) };
var validator = ValidationFactory.CreateValidator<Person>();
ValidationResults vrs = validator.Validate(p);
foreach (ValidationResult vr in vrs)
{
Console.WriteLine(vr.Message);
}
This will print: "Person must be less than 150 years old."