When you create an application it is highly important to validate the data entered by the users so that only valid data gets stored. ASP.NET applications can be made foolproof against any invalid data by using the validation controls. They are available in the tool box just like other controls. ASP.NET validation controls can be attached to the controls that must be validated before transferring data to the database. The ASP.NET validation controls can be implemented in an application with almost nil coding efforts.

Some examples of validations needed in application are

  • Checking text length e.g. maximum length for name field
  • Allowing values within a range e.g. age limit 18 to 50 years, date of birth between 01/01/2001 and 31/12/2016
  • Comparing values of two textboxes e.g. matching password and confirm password
  • Comparing value in a textbox with a constant
  • Allowing values matching a specific format like email ID, website name, file name, currency etc.
  • Disallowing blank textboxes

And so on.

Validations are most commonly implemented on client side to reduce the server side computations and reducing network traffic. It makes sure that validation is done by the client browser and only correct values are passed on to server for further processing. The validation is done according to the business rules defined by the customer organization.

ASP.NET Validation controls

There are 6 validation controls available in ASP.NET. These can be placed in a web form just like other web controls.

ASP.NET validation controls

For all these controls you have to place a web control on the form to take input from user. Next you have to place the validation control of your choice along with the control to display message in case user enters an incorrect value.

CompareValidator Validation Control

This ASP.NET validation controls are used to compare value of one control with another or with a constant value. For example you can check whether the users have entered the same password in the original and confirm text boxes. Or compare the user entered value with a specific value (constant).

You need to set five properties on the CompareValidator Validation Control

  • ControlToCompare- Name of the control whose value you want to compare with
  • ControlToValidate- Name of the control with whose value you want to validate
  • ValueToCompare- Here you have to give the constant value if you don’t want to compare with another control
  • ErrorMessage – Error message you wish to display to user if the values of the ControlToCompare and ControlToValidate or ControlToValidate and ValueToCompare do agree with comparison operator selected.
  • Operator- the comparison you wish to makeASP.NET validator operators

CustomValidator Validation Control

Most of the times, validation is taken care by the client side. If you have to implement validation check at both client and server end you can use this ASP.NET validation control. In cases where user browser JavaScript is disabled and JavaScript validation is not executed at client, validation will be done at the server end.

You need to set four properties on the CompareValidator Validation Control

  • ControlToValidate- Name of the control whose value you want to validate
  • ErrorMessage – Error message you wish to display to user on incorrect data entry
  • ClientValidationFunction- client side validation function written using a scripting language
  • OnServerValidate- server side validation function written in any Visual Studio Language. This is called when CustomerServerValidate event gets fired.

RangeValidator Validation Control

When you want the user to enter values with in a predefined range, this validation control must be used.

You need to set five properties on the CompareValidator Validation Control

  • ControlToValidate- Name of the control whose value you want to validate
  • ErrorMessage – Error message you wish to display to user on incorrect data entry
  • MaximumValue- here you set the maximum value a user can type in attached web control
  • MinimumValue – here you set the minimum value a user can type in attached web control
  • Type- you need to specify the type of data you are comparingdatatype rangevalidator

RegularExpressionValidator Validation Control

When you want data like email id, website name or phone number to be entered in a specific pattern you can use the RegularExpressionValidator.  If the value entered by the user violates the pattern defined in ValidationExpression property, the ErrorMessage is displayed

You need to set three properties on the CompareValidator Validation Control

  • ControlToValidate- Name of the control whose value you want to validate
  • ErrorMessage – Error message you wish to display to user on incorrect data entry
  • ValidationExpression- set this property with one of the given regular expression or you can create your own regular expression. To learn how to create Regular Expressions See here. When you click the small button in this property (in property window) you will get this dialogbox to select and apply your required Regular Expression.ASP.NET regular expression options

RequiredFieldValidator Validation Control

If you don’t want the user to leave a field blank you can use the RequiredFieldValidator Control. It will display error message when a user leaves a control blank.

  • ControlToValidate- Name of the control whose value you want to validate
  • ErrorMessage – Error message you wish to display to user if she leaves the control blank.

ValidationSummary Validation Control

If you want to list down all the validation error message to user for a webform use the ValidationSummary Validation Control. It can be placed after or before all the controls. On clicking the submit button on a webform,  if it contains any validation errors they can be reported to the user. The important thing is that you have to place other validation control along with web controls of the form.

Example

Design a form as shown in the image using web controls – label, Textbox, button and 6 validation controls discussed here

ASP.NET Validator Controls Design

Set the properties of all the validation controls

Validator Control Properties Values Set in property Window
RequiredFieldValidator ControlToValidate
Forecolor
ErrorMessage
Textbox1
#660066
User name must not be left blank
RangeValidator ControlToValidate
MaximumValue
MinimumValue
Type
Forecolor
ErrorMessage
Textbox2
18
60
Integer
#FF9900
Age between 18-60 years
CustomValidator ControlToValidate
ClientValidationFunction
Forecolor
ErrorMessage
Textbox3
pwdValidation
#00CC00
Password must have 8 characters
CompareValidator ControlToValidate
ControlToCompare
Type
Forecolor
ErrorMessage
Textbox4
Textbox3
String
#CC00CC
passwords do not match
RegularExpressionValidator ControlToValidate
ValidationExpression
Forecolor
ErrorMessage
Textbox5
^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$
Blue
incorrect email
ValidationSummary Forecolor Red

 

Double click customValidator control. Add the following code in the CustomValidator1_ServerValidate event

 
 
  If TextBox3.Text.Length < 8 Then
            args.IsValid = False
        Else
            args.IsValid = True
      End If

Run the web application and punch in some incorrect values. On clicking submit button you will see messages as shown in the image.

ASP.NET validation controls execution