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(null, null)
{
_domains = new List<string>(new string[] { ".com",
".net", ".gov", ".org", ".edu", ".mil" });
}
public EmailDomainValidator(params string[] acceptedDomains)
: base(null, null)
{
_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, null, this));
}
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;
}
}