Create a relative datetime validator

May 10, 2007 at 11:09 PM
I have a requirement to validate that a person's Date of birth is not null and the person is atleast 18 years of Age. I have a property called DOB for a customer and I want to validate against this requirement.

I tried to use the Relative Datetime validator using the following XML config:
<property name="DOB">
<validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.AndCompositeValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
name="And Composite Validator">
<validator negated="false" messageTemplate="Your date of birth is either missing, in an incorrect format, or indicates that you are not at least 18 years old."
messageTemplateResourceName="" messageTemplateResourceType=""
tag="775" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.NotNullValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
name="Not Null Validator" />
<validator lowerUnit="Year" upperUnit="Year" lowerBound="18"
lowerBoundType="Inclusive" upperBound="0" upperBoundType="Ignore"
negated="false" messageTemplate="Your date of birth is either missing, in an incorrect format, or indicates that you are not at least 18 years old."
messageTemplateResourceName="" messageTemplateResourceType=""
tag="775" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.RelativeDateTimeValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
name="Relative Date Time Validator" />
</validator>
</property>


But, when I input the DOB as "1977-07-08", validation fails although the person is more than 18 years. What am i doing wrong?

Thanks,
Rajesh

May 11, 2007 at 11:46 PM
Check the documentation for an example.

The example in the documentation for the RelativeDateTimeValidator shows how to validate a birthdate to check if someone is over 18yrs old.

Regards,

Dave

__________________________

David Hayden
Microsoft MVP C#