Skip to main content
Email Password   helpLost your password?

Sample Image - Validator.gif

Introduction

I've been playing with ASP.NET since Beta 1, and I have to admit that I love it! What used to take days in traditional ASP can now be done in an afternoon. One of the few complaints that I have is ASP.NET's implementation of validators. While I certainly believe that validation controls extremely are time saving, I'm not really thrilled with the ones provided by Microsoft. They work great if you only have a few items to validate, but can become a chore to maintain, when there are many fields that need validation.

What I wanted was a single control that you could drop on to your form, that would handle all the validation for you. This way, you have one validator to maintain, not 10, or whatever. So I wrote this custom Validator control. Included in the download is the Validator control itself, its source code (don't run away screaming now, it's in VB.NET...), and 3 JavaScript files. I couldn't find an elegant way to use Microsoft's JavaScript files, and they didn't seem to support the DOM specs, so I wrote my own files. These need to be placed in your \inetpub\wwwroot\_vti_script directory. Validation.js is the main JavaScript file, which sets up some prototype functions, then links to one of the other two files depending on your browsers capability. ValidationDOM.js is, of course, for DOM compliant browsers (NS6.2+, IE5+). ValidationIE.js is for earlier versions of IE. I chose not to support early versions of Netscape, because there doesn't seem to be an elegant version of document.getElementById or document.all, and so Validation.js simply forces early versions of Netscape to use server side validation only.

Using the control

That being said, an explanation of its use is in order. The Validator control is derived from System.Web.UI.Webcontrols, and so has the standard properties of a web control. It also has 4 additional properties:

Once the Validator has been added to a page, you need to tell it what to validate. By either right-mouse clicking on the control, or looking at the designer verbs area just below the property window, you'll notice a menu/verb item marked 'Edit Fields'. A dialog then appears (as shown in the screenshot) allowing you to modify the control's collection of validations. Clicking on one of the buttons, adds a validator to the list, while clicking Remove will, of course, remove the selected item. All validation options share some common properties:

Next is a summary of the additional properties of each of the validation types:

In order to use server side validation for the CustomFieldValidator, you need to handle the CustomFieldValidation event of the Validator object. All CustomFieldValidator items that you've added to the control will route through this same event, so you need a way to distinguish between them. This is where the Name property of the CustomFieldValidator comes in hand. It is sent in the CustomFieldValidatorEventArgs class to the event handler.

Public Sub Validator1_CustomFieldValidation(ByVal sender As Object, _
         ByVal E As CP.Validator.CustomFieldValidatorEventArgs) _
         Handles Validator.CustomFieldValidation
    Select Case E.Name
        Case "IsEven"
            If CInt(DirectCast(E.ControlToValidate, _
                         TextBox).Text) Mod 2 = 0 Then
                E.IsValid = True
            Else
                E.IsValid = False
            End If
    End Select
End Sub

To add a validation to the control at run time, use the Validators collection of the control:

Validator1.Validators.Add(New ReqFieldValidator("txtName", 
                                       "Enter a name", ""))

I've tested it every way that I could think of, and it seems to be stable. However, since this is the control's first release to the public, you should expect bugs. If you run across any, please either post them here, or E-mail me directly at jamie.nordmeyer@mcgnw.com, and I'll do my best to fix it. Also, if you have any comments, concerns, or suggestions, let me know.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralPray tell, what does this article have to do with Windows Forms? Pin
cjard
3:11 14 Jun '09  
GeneralRe: Pray tell, what does this article have to do with Windows Forms? Pin
tonymontana82
11:29 15 Jul '09  
GeneralQuestion and suggestion Pin
Evert Wiesenekker
3:46 17 Apr '07  
GeneralThe Perfect Solution Pin
kooks2k
21:18 31 Jan '07  
GeneralDataGridView Pin
DanielRabe
6:48 28 Nov '06  
Generalerror provider Pin
shabonaa
10:18 20 Oct '06  
GeneralThanks a lot! Pin
Piotr Nogalski
3:18 7 Oct '06  
GeneralRemoving icon, when disabling Control Pin
drJoene
8:37 2 Oct '06  
GeneralNice, but... Pin
twesterd
12:14 7 Jun '06  
GeneralRe: Nice, but... I'm looking forward to your article... Pin
Peter Hancock
6:27 23 Aug '06  
GeneralRe: Nice, but... I'm looking forward to your article... Pin
Brisbane
20:56 13 Jun '07  
GeneralRe: Nice, but... Pin
Zoodor
6:34 14 Sep '06  
GeneralRe: Nice, but... Pin
Paul Menefee
7:13 26 Sep '06  
GeneralRe: Nice, but... Pin
Pink Floyd
8:19 2 Nov '06  
GeneralValidating UserControl Pin
Ali Youssef
6:55 27 Apr '06  
GeneralRe: Validating UserControl Pin
dwegmann
13:20 19 May '06  
GeneralRe: Validating UserControl Pin
DanielRabe
6:58 10 Oct '06  
QuestionIcon Pin
zizkam
4:10 27 Apr '06  
AnswerRe: Icon Pin
Rick Engelking
6:18 27 Apr '06  
JokeVery cool Pin
davepermen
22:18 26 Apr '06  


Last Updated 26 Apr 2006 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009