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

Updated ASP.NET Page

The updated ASP.NET page now looks like it does below.  I only included the two new properties.  Note that it looks the same as the other properties from the previous example.

Listing 10

<tr>
<td>Zip Code</td>
<td>
  <asp:TextBox ID="txtZipCode" runat="server" />
  <el:PropertyProxyValidator ID="ppvZipCode" 
   runat="server"  
   SourceTypeName="Mains.Examples.User,App_Code"
   PropertyName="ZipCode" RulesetName="primary" 
   ControlToValidate="txtZipCode">
   *</el:PropertyProxyValidator>
</td>
</tr>
<tr>
<td>Authorization Code</td>
<td>
  <asp:TextBox ID="txtAuthorizationCode" runat="server" />
  <el:PropertyProxyValidator ID="ppvAuthorizationCode" 
   runat="server" 
   SourceTypeName="Mains.Examples.User,App_Code"
   PropertyName="AuthorizationCode" RulesetName="primary" 
   ControlToValidate="txtAuthorizationCode">
   *</el:PropertyProxyValidator>
</td>
</tr>

When performing the validation on an empty form, the following error messages are rendered in the browser through the validation summary control.

·         The name must have at least a space between the names.
The name must be between 3 and 150 characters long.

·         The phone number must be between 7 and 10 characters long.
The phone number is not a valid phone number; it can be numbers only.

·         The email must have at least an @ sign and at least one decimal.
The domain of the email address was not determined.
The email address must be between 7 and 150 characters long.

·         The zip code does not exist in the allowable list of zip code values; it must be in a range of 15601-15609.
The zip code entered is not five digits.

·         The authorization code must begin with the first letter of "A."
The authorization code is outside the range of valid values.

All of the new validations work, and as you enter in data, it performs the validation correctly.  This is to illustrate the power of the validation framework in Enterprise Library 3.

Note: This code was created from the Community Technology Preview (CTP) version of Enterprise Library 3.  It may vary from the final release of the product.  Although this version of the Framework is very close to the final, it may vary slightly from the final version.


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 7:29:54 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search