Working with Custom Validators using Enterprise Library 3
page 4 of 8
by Brian Mains
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 33538/ 82

ValidZipCodeValidator Example

For the next example, the DomainValidator base class validates items that exist in a list, passed to it through the object's constructor.  The list has to be defined statically so that it can be available at the time of construction of the object.  This list is available at the time of validation, so if the item does not exist in the list, then the validation fails.  This occurs in the DoValidate method for us, and no coding effort is needed.

First, we work with a static array of zip codes, which will be used to validate the data. Assume that in the form we only have a limited list of zip codes that are allowed; so only users in that zip code can work with the form. If they enter in a value outside of the zip code array, an error is generated.  Although the form could use a drop down box, for the sake of the example, we are using a textbox, which means the zip code property is a free-form text property and we need proper validation setup.  We begin with the list of values.

Listing 4

private static List<string> ZIPS = 
 new List<string>(new string[] { "15601""15602", 
 "15603""15604", "15605""15607""15607""15608",
 "15609" });

This list gets passed through the constructor as shown below.  Because the list is static, it is available at construction.

Listing 5

public ValidZipCodeValidator() : base(ZIPS) { }

DoValidate does not need to be overridden because it validates the list properly, but it could be if desired.  The new zip code property definition appears below.  The validator attribute takes no constructors, so no additional properties, except for the optional ones, need to be provided.

Listing 6

[RegexValidator(@"\d{5}""The zip code entered is not five digits", Ruleset =
  "primary"), ValidZipCodeValidator(Ruleset = "primary")]
public string ZipCode
    return _zipCode;
    _zipCode = value;

View Entire Article

User Comments

Title: mmm Reply   
Name: Brian
Date: 2008-06-23 10:46:18 AM
Validator doesn't inherit from that class, correct. You have to create an accompying attribute that works along side of it. Create another class that inherits from ValidatorAttribute (the attribute class). It has a method you need to override that uses your validator to validate the data. Sorry, I didn't include it in the article.
Title: mmm   
Name: John
Date: 2008-06-21 11:58:45 PM
Base class Validator don't inherit System.Attribute. How can i use it in this way:
[StringLengthValidator(7, RangeBoundaryType.Inclusive, 150,
"The email address must be between 7 and 150 characters",
Ruleset = "primary")
, ContainsCharactersValidator("@.", ContainsCharacters.All,
"The email must have an @ and at least one period",
Ruleset = "primary"),
EmailDomainValidator(".com", ".net", ".edu", ".gov", ".biz", ".tv",
Ruleset =

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2021  |  Page Processed at 2021-12-05 12:33:46 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search