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
ASP.NET validators are ASP.NET controls that when associated with a web control will perform client-side (JavaScript) and server-side (C#/VB.NET) validation.
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
You will need a class library to host your validator, this class library will be used to add additional validators in the future. The library will contain a JavaScript file that will include the required JavaScript and will be served whenever the validator is used.
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
The previous line will append the prefix “at” to the validator control when dropped on a form.
Add the JavaScript file
This is the JavaScript file that will be served whenever our validator is used on a page. We will embed this file as a 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 .
Now add a JavaScript file and call it JavaScript.js then change its build action to be “Embedded Resource.” In the AssemblyInfo.cs file you need to add a line that will make this JavaScript file a 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:
And the JavaScript.js file:
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: AddAttributesToRender
, EvaluateIsValid
and OnPreRender
.
The JavaScript in our case matches the C# code to do the same check on the client. Note that the JavaScript is using ValidatorTrim
and ValidatorGetValue
which are methods that are available to you via the framework.
The reason behind both server and client sides checking is because:
- The user will have an old browser or JavaScript turned off, so in all cases, the server-side check would still execute. You don’t want to be at the mercy of your web users, do you?
- The JavaScript will help informing the user as soon as he changes focus or clicking submit that his entry is invalid and saves the page to do a round trip to the server.
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
section:
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 AT.Web.UI.Validators.dll
.
Drop it on the page
Add the following code to the “default.aspx” page.
Download
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.
Credit Card ASP.NET Validator (11 kb)
Disclaimer
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.
UPDATE: How Not To Compromise Security Through ASP.NET Validators.