Introduction to Validation Controls

  • Validation is an important concept to get a correct output as the user gives the input to web form must be in proper format then only the server will result in meaningful output.
  • Data entered by the user must perform validation before sending to the server for processing.


“Validation is checking whether the user has entered correct data or input or not and then process the request and send it to the server for output.”

ASP.NET Validation Controls


Validation can be performed at the client-side as well as server-side.

  • Client-side validation makes the process fast as there is less number of hits to the server.
  •  Server-side validation is used to remove the limitation of client browsers dependencies and scripting language support. 

Types of Validation

Validation can be classified into two types based on client and server validation:

  1. Client side Validation
  2. Server side Validation
ASP.NET Validation Controls 1
  1. Client side Validation
  • Validations performed  at Client-Side i.e., browser.
  • User gets information immediately as validation performed before a request is sent to the server. 

2.  Server-side Validation

  • Server-side validation takes place before data processing in the database.
  • This type of validation is used, where client browser dependencies are involved.

Hierarchy Model of ASP.NET Validation controls

  • All the ASP.NET Validation controls derive from the WebControl class.
  • The WebControl class derive from System.Web.UI.Control, which itself derives from System.Object
ASP.NET Validation Controls 2
  • ASP.NET validation controls are derived from the abstract class i.e., BaseValidator Class, which is derived from Label Control class.

BaseValidator Class

BaseValidator class is parent for all ASP.NET validation controls.

It is abstract class means we cannot directly instantiate BaseValidator.

All ASP.NET Validation controls inherit methods and properties of BaseValidator class.

Some common and essential properties provided by the BaseValidator Control class are as follows:

Properties Description
ControlToValidate Which control to be validate.
Display A Mode in which error message is shown like static, dynamic, none.
ErrorMessage Error message to be displayed.
Text If validation fails, the message to be shown.
SetFocusOnError Focus is set to the control having incorrect values


  • Property named ControlToValidate cannot be left blank.
  • Multiple validation controls can be assigned to a webform input control.
  • CauseValidation property of the control must be “true” only then server validation will take place.

Validation Controls used in ASP.NET

The Validation controls used in APS.NET Web Form are as follows:

  1. ValidationSummary
  2. RequiredFieldValidator
  3. RangeValidator
  4. CompareValidator
  5. RegularExpressionValidator
  6. CustomValidator

Validation Controls can be used at the client-side as well as server-side.

  1. ValidationSummary Control :
  2. ValidationSummary Control is used tosummarize all the error message at one place in the list, bullet, or single message form.
  3. Some important properties of ValidationSummary Control are DisplayMode, ShowMessageBox, ShowSummary,
  4. RequiredFieldValidator Control:
  • Control used to make web form input control required i.e. it cannot be left empty.
  • User will have to enter some input without which form will not be transferred to the server.

Code for aspx page

ASP.NET Validation Controls 3


InitialValue : The property of RequiredFieldValidation control will check in the associated control to validate whether the value is same as InitialValue or not. If a value matches to IntitalValue, it is invalid, value other than InitialValue is Valid.

  • RangeValidator Control
  • RangeValidator Control is used to apply some upper and lower limit for associated input control.
  • Value in input control is checked with Maximum and Minimum property values.
  • Type property of RangeValidator Control defines which type of comparison takes place.
ASP.NET Validation Controls 4
  • CompareValidator Control
  • CompareValidator is used to compare some value of control with some constant value or any value within different control.
  • When we have to compare the value of one input control to others, we need to provide value for CotrolToCompare property.
  • If the value is to be compared with some constant, ValueToCompare must be given.

Some important properties of CompareValidator are ControlToCompare , ControlToValidate , Type , Operator ,Display etc.

ASP.NET Validation Controls 5
  • RegularExpressionValidator Control
  • RegularExpression Validator is used when the input from the user is in a standard pattern, and that pattern must be followed to prevent wrong entry to the database.
  • The pattern specified in ValidationExpression property is validated then only process proceeds.

Frequently used Currency formats:

To validate a positive currency. Example(1.00)  \d+(\.\d\d)?$
To validate a positive or negative currency. Example (1.20) ^(-)?\d+(\.\d\d)?$

Frequently used string formats:

To validate only characters ^[a-zA-Z]*$
To validate character or numbers and character only, not space, not special character. Example :-Rajpal123 ^[a-zA-Z0-9]*$
To validate min 8 and max 20 character with spaces. Example :-  ram 234 ([a-zA-Z0-9\s]){8,20} 

Frequently used Number Formats:

To validate Float number with + Or – Symbol. Example 1.222 Or -1.222 ^[-+]?[0-9]*\.?[0-9]*$
To validate user can input only number but no limit. Example :- 125465896 ^(\d+)?$
To validate only three digit. Example:- 123 ^(\d\d\d)?$

Some File uploader Regular expression Formats:

To validate format for file uploading Example:- abc.jpg


5.  CustomValidator Control

CustomValidator is used when the validation is performed to match the user’s defined standards.

CustomValidator allows you to perform validation from both the client-side as well as server-side.

ASP.NET Validation Controls 7

Pin It on Pinterest

Share This