views:

116

answers:

2

Hello All!

How to validate DateTime by Validation Application Block, to not be more than DateTime.Now?

thanks.

A: 

You can use Regex for validation just choose time format http://regexlib.com/REDetails.aspx?regexp_id=504

Xenon
The question is about Validation Application Block that is part of Enterprise Library.
Vadim
@Vadim i see but it more easier with regex
Xenon
@Xenon, the question is not about the format...it's about having a DateTime value less than or equal to another DateTime value.
Tuzo
A: 

I think the easiest way to do this using the Validation Application Block would be to create a property that returns DateTime.Now and then use a Property Comparison Validator.

SelfValidation would also let you do what you want. The Property Comparison Validator lets you externalize the logic but it feels a bit wrong to expose a property just to compare against.

Implementation

public class Person
{
    public DateTime Now
    {
        get { return DateTime.Now; }
    }

    public DateTime BirthDate
    {
        get;
        set;
    }
}


<type assemblyName="MyApp.Entities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="MyApp.Entities.Person">   
  <ruleset name="MyRuleSet">
    <properties>
      <property name="BirthDate">
        <validator operator="LessThanEqual" propertyToCompare="Now" negated="false"
          messageTemplate="I don't know nothin' about birthin' no babies in the future." messageTemplateResourceName=""
          messageTemplateResourceType="" tag="" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.PropertyComparisonValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null"
          name="Property Comparison Validator" />
      </property>
    </properties>   
  </ruleset> 
</type>
Tuzo