The Three Steps of Building an ASP.NET Validator Control
The standard ASP.NET validator controls such as the
RequiredFieldValidator or the
RegularExpressionValidator do not cover all validation requirements, so usually developers tend to create a
CustomValidator for such scenarios.
A major problem with the
CustomValidator is reusability, as if you wanted to use the validator in another project then there would be some copying and pasting and code duplication, then you have to maintain multiple versions of the same control.
The solution, as you have guessed from the title, is to build your own validator control when possible to promote reusability.
In this post I will be showing you in three simple steps how to build an ASP.NET validator control and take credit card number format check to show by example. I will also be building the architecture so that your validator and other validators that you will develop in the future could be as reusable as possible.
How to Check a Credit Card Format
Luhn check is an algorithm that checks if a credit card number is valid (format wise), so in practice, before you even think of doing any further processing on the credit card, this check should be satisfied.
Introduction to ASP.NET Validators
Using them will dramatically reduce the amount of validation code and will helps in writing more maintainable and clean code. The more specialised controls that you have the more you will finish building a form faster.
All what you need to do is check for
Page.IsValid in the server-side (in our case that would be the code-behind file) to make sure that the page is valid before you start collecting its values.
Step 1: Prepare The Class Library to Include the Validators
Add The Class Library
Create a new class library, mine is called
AT.Web.UI.Validators then add a reference to
System.Web and remove the auto generated class
class1 from the project
You will need to add this line to the end of the AssemblyInfo.cs
[assembly: TagPrefix("AT.Web.UI.Validators", "at")]
The previous line will append the prefix “at” to the validator control when dropped on a form.
WebResource in the library to save ourselves adding it manually to every page that uses our validator.
To know more about
WebResources have a look at WebResource ASP.NET 2.0 explained.
WebResource, first add a reference to the name space
using System.Web.UI; then append this line to the end of the file:
Step 1 will only be done once and the next time you add a new validator you won’t need to do it again.
Step 2: Write the ASP.NET Validator
Add to the project a class file and call it CreditCardValidator.cs. The code that should go in the class looks like the following, I will discuss the code later:
What is this code all about?
When implementing ASP.NET validators, your best candidate to inherit from is the
BaseValidator. And you should always, at least, override these three methods:
ValidatorGetValue which are methods that are available to you via the framework.
The reason behind both server and client sides checking is because:
Step 3: Use the Validator Control
Now that you have created the validator you will need to test it and use it. So, create a web project and then add the class library that you have created in Step 1 to the solution. Add a reference from the web project to the class library project (not to the .dll).
You will need to add this line to the web.config inside your
<system.web> <pages> <controls> <add tagPrefix="at" namespace="AT.Web.UI.Validators" assembly="AT.Web.UI.Validators"/> ...
This will instruct ASP.NET that whenever there is a tag that is prefixed with “at” then it should be fetched from namespace
AT.Web.UI.Validators that exists in assembly
Drop it on the page
Add the following code to the “default.aspx” page.
<asp:TextBox runat="server" ID="CreditCardNumber" /> <at:CreditCardValidator runat="server" ControlToValidate="CreditCardNumber" Text="Invalid credit card number. Please recheck." /> <br /> <asp:Button runat="server" ID="Submit" onclick="Submit_Click" Text="Submit" />
For a list of test credit card numbers try this Credit Card Test Numbers
I have added the source code with Visual Studio 2008 solution and the assembly file using .NET Framework 2.0, feel free to download it and use it. However, all copyrights should be given when needed.
I tried to ensure that the information posted here are up to date and accurate, however, I do not hold any responsibility to any damages that might occur from using or misusing the information and the posted code.
What is next?
In the next post I will be discussing the validators secrity holes… If you liked what you’ve seen so far, then let me know in a comment so I would discuss it in the next post. And finally, Kick It if you like it!