Share via


Date Time Range Validator

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.

Class Name: DateTimeRangeValidator

Attribute Name: DateTimeRangeValidatorAttribute

Configuration tool name: Date Time Range Validator

Description

This validator checks that a DateTime object falls within the specified range.

Properties

The following table lists the Date Time Range Validator properties.

Property

Description

LowerBound

This is the low-range boundary value. It must be of type DateTime. The compiler checks this requirement if you directly invoke the validator in your code. If you use attributes, the application block throws an exception at run time if the type is incorrect. If you use configuration, the configuration tool displays an error if you enter an incorrect type.

LowerBoundType

This property determines how to evaluate the LowerBound value. Possible values for LowerBoundType are Ignore, Inclusive, and Exclusive. The Ignore value means that the validator ignores the LowerBound value. This is the default. The Inclusive value means that the validator allows values that are equal to the LowerBound value. The Exclusive value means that the validator does not allow values that are equal to the LowerBound value.

MessageTemplate

This property is a string containing template tokens that the validator replaces with values as it validates the target. Typically, it describes the validation result.

MessageTemplateResourceName

If you do not want to use the MessageTemplate property to hard-code a message template (perhaps for internationalization), you can use a template stored in the application resources. You must also specify a MessageTemplateResourceTypeName value. If you include both a MessageTemplate value and a MessageTemeplateResourceName value, the MessageTemplate value takes precedence.

MessageTemplateResourceTypeName

The resource type for the template you want to use. If you specify a MessageTemplateResourceName value, you must specify this value.

Tag

This property is a user-supplied string. Typically, it is used to sort or categorize validation results.

UpperBound

This is the upper-range boundary value. It must be of type DateTime. The compiler checks this requirement if you directly invoke the validator in your code. If you use attributes, the application block throws an exception at run time if the type is incorrect. If you use configuration, the configuration tool displays an error if you enter an incorrect type.

UpperBoundType

This property determines how to evaluate the UpperBound value. Possible values for UpperBoundType are Ignore, Inclusive, and Exclusive. The Ignore value means that the validator ignores the UpperBound value. This is the default. The Inclusive value means that the validator allows values that are equal to the UpperBound value. The Exclusive value means that the validator does not allow values that are equal to the UpperBound value.

Message Template Tokens

If the message template contains tokens (for example, "{0}"), the validator will replace these tokens with values when the ValidationResult is created. The following table lists the tokens that are supported by the Date Time Range Validator.

Token

Meaning

{0}

This token represents the value of the object that is being validated. Although it can be useful to show the original value as a part of the validation message, you must be careful to avoid injection attacks by escaping any characters that can be used to attack the system that conveys the message to the user.

{1}

This token represents the key of the object that is being validated. When the validator is attached to a member of a type such as a property or a field, the key is set to the member name. When the validator is attached to an object, the key is null and the token is replaced by an empty string.

{2}

This token represents the tag that is specified on the validator instance. If no tag is supplied, the token is replaced by an empty string.

{3}

The lower bound configured for the validator instance.

{4}

The lower bound type (Inclusive, Exclusive, or Ignore) configured for the validator instance.

{5}

The upper bound configured for the validator instance.

{6}

The upper bound type (Inclusive, Exclusive, or Ignore) configured for the validator instance.

Example

The following code example checks that the expiration date is before January 20, 2010.

public class Product
{
  [DateTimeRangeValidator("2010-01-20T00:00:00")]
  public DateTime ExpirationDate
  {
    get
    {
      return expirationDate;
    }
  }
  // ...
}
'Usage
Public Class Product
  <DateTimeRangeValidator("2010-01-20T00:00:00")> _
  ReadOnly Property ExpirationDate(ByVal _expirationDate As DateTime)
    Get
      Return _expirationDate
    End Get
  End Property
  ' ...
End Class