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

EmailAddressDomainValidator Example

The first example I created was an email address domain validator, which will inherit from the Validator base class.  For instance, only so many domains may be considered as acceptable for use in our application, such as .com, .net, .gov, .mil, etc.  Any other type of email addresses will require that the user use an alternative email address.  While this may not be a valid example, it does bring up the possibility that we can perform advanced validation capabilities within the domain.  The default constructor defines several domains considered by default, but an overload allows the developer to specify the domains they want to use.  The constructors are defined as follows:

Listing 1

public EmailDomainValidator()
      : base(nullnull)
{
      _domains = new List<string>(new string[] { ".com"".net"".gov"".org"".edu", ".mil" });
}
 
public EmailDomainValidator(params string[] acceptedDomains)
      : base(nullnull)
{
      _domains = new List<string>(acceptedDomains);
}

 

The DoValidate method is where the real work begins.  Based upon the list of accepted domains, the validator will capture the last four characters of the domain and process it.  If the domain is not in the list, then a message is added to the list of validation results.

Listing 2

protected override void DoValidate(string objectToValidate, object
  currentTarget, string key, ValidationResults validationResults)
{
  if (!string.IsNullOrEmpty(objectToValidate) && objectToValidate.Length >= 6)
  {
    string extension = 
 objectToValidate.Substring(objectToValidate.LastIndexOf
      ('.'));
 
    if (!_domains.Contains(extension))
      validationResults.AddResult(new ValidationResult(@
        "The email address domain is not acceptable ", 
 currentTarget, key, null,
        this));
  }
  else
    validationResults.AddResult(new ValidationResult(@
      "The email address must be longer ", 
 currentTarget,key, nullthis));
}

The objectToValidate parameter stores the actual value being validated, which is the parameter that is used to perform the validation work.  The total email address must be six characters, assuming two characters for the "at" sign and period, two characters for the domain, and at least one for the domain and address.  If it meets this, the extension is processed from the last decimal point, and if the domain is not valid, a new result is added to the collection with all of the error information.

The validation result collection takes a ValidationResult object.  This object's constructor defines parameters that are the message to display, the current target, the key, a tag (if provided), and the validator that is validating it.  Note in this example, currentTarget is the PropertyProxyValidator object performing the validation, since we are in an ASP.NET context, and the key value is the name of the business property being validated.

The new property definition for Email is shown below.  I used the overload for the EmailDomainValidator attribute constructor to illustrate how it will appear in the property definition.

Listing 3

[StringLengthValidator(7, RangeBoundaryType.Inclusive, 150,
  RangeBoundaryType.Inclusive,
  "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 =
  "primary")]
public string Email
{
  get
  {
    return _email;
  }
  set
  {
    _email = value;
  }
}

View Entire Article

User Comments

Title: mmm Reply   
Name: Brian
Date: 2008-06-23 10:46:18 AM
Comment:
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
Comment:
Base class Validator don't inherit System.Attribute. How can i use it in this way:
[StringLengthValidator(7, RangeBoundaryType.Inclusive, 150,
RangeBoundaryType.Inclusive,
"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 =
"primary")]
????

Product Spotlight
Product Spotlight 





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


©Copyright 1998-2021 ASPAlliance.com  |  Page Processed at 2021-11-29 6:11:19 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search