ASP.NET Validation Controls

7/29/2009 6:24:17 PM

Recently we have become extremely busy at work. I have been doing a large amount of work around the mark up pages. I needed to apply some validation around the text boxes on a page. Some of the validation that I required was making sure there is a value in the text box, making sure that the value in two text boxes match and making sure that the email that the user has entered is valid.

There are six controls included with ASP.NET. They are:

  • The RequiredFieldValidation Control
  • The CompareValidator Control
  • The RangeValidator Control
  • The RegularExpressionValidator Control
  • The CustomValidator Control

All of the validation controls inherit from the base class BaseValidator. So they all have a series of properties and methods that are common to all validation controls. These properties and methods include:

  • ControlToValidate - This value is which control the validator is applied to.
  • ErrorMessage - This is the error message that will be displayed in the validation summary.
  • IsValid - Boolean value for whether or not the control is valid.
  • Validate - Method to validate the input control and update the IsValid property.
  • Display - This controls how the error message is shown. Here are the possible options:
  • None (The validation message is never displayed.)
  • Static (Space for the validation message is allocated in the page layout.)
  • Dynamic (Space for the validation message is dynamically added to the page if validation fails.)

The RequiredFieldValidation Control

This ensures that the user has entered a value.
 Required field:    

The CompareValidator Control

This ensure that values in two controls are the same.
 Textbox 1: 
Textbox 2:

The RangeValidator Control

This ensures that the value that the user enters is between a specific range.
 Enter a date from 1998:  * 

The RegularExpressionValidator Control

This ensures that the user enters an expression that you specify.
 E-mail:  *  

The CustomValidator Control

This is a custom control where you can specify what exactly you want the user to enter.
 Field:  *