---
title: "Validation controls in ASP.Net"  
description: "A Validation control is used to validate the data of an input control. If the data does not pass validation, it will display an error message to the u"  
author: "Anonymous User"  
published: 2010-07-21  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/43/validation-controls-in-asp-dot-net  
category: "asp.net"  
tags: ["asp.net"]  
reading_time: 3 minutes  

---

# Validation controls in ASP.Net

A Validation control is used to validate the data of an input control. If the data does

not pass validation, it will display an error message to the user.\

##### There are six types of validation controls.

· RequiredFieldValidator

· RangeValidator

· RegularExpressionValidator

· CompareValidator

· CustomValidator

· ValidationSummary

##### RequiredFieldValidator

The RequiredFieldValidator control is used to make an input control a required

field.

This control will display error message if the field on which validation control is

applied is left blank.

##### Example

```
<form id="form1" runat="server">    <asp:TextBox ID="TextBox2" runat="server"        style="z-index: 1; left: 64px; top: 88px; position: absolute" TabIndex="3"></asp:TextBox>    <asp:TextBox ID="TextBox1" runat="server"        style="z-index: 1; left: 63px; top: 51px; position: absolute" TabIndex="1"></asp:TextBox>       <asp:Button ID="Button1" runat="server"        style="z-index: 1; left: 68px; top: 122px; position: absolute"        Text="Button" />   //applying RequiredFieldValidator to TextBox1       <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"        ErrorMessage="* You must enter a
value into textbox"        style="position:absolute; top: 51px; left: 220px;" Display="Static">*</asp:RequiredFieldValidator> //applying
RequiredFieldValidator to TextBox2       <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox2"        ErrorMessage="* You must enter a
value into textbox"        style="position:absolute; top: 87px; left: 222px;" Display="Static">*</asp:RequiredFieldValidator>       </form>
```

##### Screen shot

![Validation controls in ASP.Net](https://www.mindstick.com/mindstickarticle/3af3cb8e-3bc0-4fc1-a2cd-8ce971c65ef2/images/4ee5fa00-aa91-4fdd-9c66-35abb6f331b2.png)

##### RangeValidator

The RangeValidator control is used to check that the user enters an input value that

falls between two values. It is possible to check ranges within numbers, dates, and

characters.

##### Example

```
<form id="form1" runat="server">    <asp:TextBox ID="TextBox1" runat="server"        style="z-index: 1; left: 60px; top: 55px; position: absolute"></asp:TextBox>    <asp:Button ID="Button1" runat="server"        style="z-index: 1; left: 96px; top: 109px; position: absolute" Text="Button" /> //applying range
validator on TextBox1. Range should be between 20 to 50, otherwise it will
display error message.     <asp:RangeValidator ID="RangeValidator1" runat="server"        ErrorMessage="* value should be
between 20 to 50"        style="position:absolute; top: 159px; left: 60px; width: 209px;"        ControlToValidate="TextBox1" MaximumValue="50" MinimumValue="20" Type="Integer"></asp:RangeValidator>    </form>
```

##### Screen shot

![Validation controls in ASP.Net](https://www.mindstick.com/mindstickarticle/3af3cb8e-3bc0-4fc1-a2cd-8ce971c65ef2/images/3b056027-1bbe-4913-9b35-6e2d5ad10c88.png)\

![Validation controls in ASP.Net](https://www.mindstick.com/mindstickarticle/3af3cb8e-3bc0-4fc1-a2cd-8ce971c65ef2/images/e9bbb28c-99f8-4483-bbed-6c90b8410cd9.png)\

RegularExpressionValidator

The RegularExpressionValidator control is used to ensure that an input value matches a specified pattern. Both server- and client-side validation are performed unless the browser does not support client-side validation or the EnableClientScript property is set to false.

This validation does not fail if field is left empty; in order to check for empty field we have to use RequiredFieldValidator control.

##### Example

```
<form id="form1" runat="server">    <asp:TextBox ID="TextBox1" runat="server"               style="z-index: 1; left: 101px; top: 45px; position: absolute; width: 197px;"></asp:TextBox>    <asp:Label ID="Label1" runat="server" Text="E-mail"        style="position:absolute; top: 48px; left: 32px; width: 92px;"></asp:Label> //applying
RegularExpressionValidator to TextBox1 to check for valid email address
provided by the user.     <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Invalid Email
ID"        style="position:absolute; top: 120px; left: 31px; width: 251px;"        ControlToValidate="TextBox1"        ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>    <asp:Button ID="Button1" runat="server"        style="z-index: 1; left: 113px; top: 83px; position: absolute" Text="Button" />    </form>
```

##### Screen shot

![Validation controls in ASP.Net](https://www.mindstick.com/mindstickarticle/3af3cb8e-3bc0-4fc1-a2cd-8ce971c65ef2/images/da295f37-fd3c-453b-94b4-f4138a49e023.png)

![Validation controls in ASP.Net](https://www.mindstick.com/mindstickarticle/3af3cb8e-3bc0-4fc1-a2cd-8ce971c65ef2/images/b8d00571-7630-4859-9010-b3a4cd7464e5.png)\

CompareValidator

The CompareValidator control is used to compare the value of one input control to

the value of another input control or to a fixed value.

This validator does not fail if the field is empty.

##### Example

```
<form id="form1" runat="server">    <asp:TextBox ID="TextBox2" runat="server"        style="z-index: 1; left: 30px; top: 100px; position: absolute"        TabIndex="3"></asp:TextBox>    <asp:TextBox ID="TextBox1" runat="server"        style="z-index: 1; left: 30px; top: 53px; position: absolute" TabIndex="1"></asp:TextBox>    <asp:Button ID="Button1" runat="server"        style="z-index: 1; left: 58px; top: 149px; position: absolute" Text="Button" /> //compare validator is
applied to TextBox2, which will compare the value of TextBox2 with the value of
TextBox1.     <asp:CompareValidator ID="CompareValidator1" runat="server"        ErrorMessage="Invalid
Value" ControlToValidate="TextBox2" ControlToCompare="TextBox1"        style="position:absolute; top: 190px; left: 25px; width: 228px;"></asp:CompareValidator> //compare validator is
applied to TextBox1, value of TextBox1 is compared with 50 with LessThanEqual
operator, so as to//check the value in
TextBox1 should not exceed 50     <asp:CompareValidator ID="CompareValidator2" runat="server"        ErrorMessage="Value should not
be greater than 50"        style="position:absolute; top: 53px; left: 200px; width: 223px;"        ControlToValidate="TextBox1" Operator="LessThanEqual" ValueToCompare="50"        Type="Integer"></asp:CompareValidator>    </form>
```

##### Screen Shot

![Validation controls in ASP.Net](https://www.mindstick.com/mindstickarticle/3af3cb8e-3bc0-4fc1-a2cd-8ce971c65ef2/images/36e9a5b2-1ada-4a6b-b154-38c325e3aaa6.png)

![Validation controls in ASP.Net](https://www.mindstick.com/mindstickarticle/3af3cb8e-3bc0-4fc1-a2cd-8ce971c65ef2/images/bcfe1d0f-8fd9-424b-98c4-62f988894c21.png)\

##### CustomValidator

CustomValidator control allows us to write method to handle validation of value entered.

#### Example

```
//code in .aspx file <form id="form1" runat="server">    <asp:TextBox ID="TextBox1" runat="server"        style="z-index: 1; left: 114px; top: 48px; position: absolute"></asp:TextBox>    <asp:Button ID="Button1" runat="server" onclick="Button1_Click"        style="z-index: 1; left: 149px; top: 95px; position: absolute" Text="Button" /> //applying custom
validation on TextBox1, and ‘CustomValidator1_ServerValidate’ method is used to
validate input.     <asp:CustomValidator ID="CustomValidator1" runat="server"        ControlToValidate="TextBox1" ErrorMessage="Invalid Number" onservervalidate="CustomValidator1_ServerValidate"></asp:CustomValidator>       </form>//code in .cs fileprotected void Button1_Click(object sender, EventArgs e)        {//this will check for
page validation.            if (Page.IsValid)                Response.Write("Valid Input");         }//method which will be
called when validation is required. protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)        {//checking the value
supplied by the user, to be between 20 to 50            if (int.Parse(args.Value) >=
20 && int.Parse(args.Value) <= 50)//if value is true
assigning true to IsValid property of arguments otherwise false.                args.IsValid
= true;            else                args.IsValid
= false;        } 
```

##### Screen shot

![Validation controls in ASP.Net](https://www.mindstick.com/mindstickarticle/3af3cb8e-3bc0-4fc1-a2cd-8ce971c65ef2/images/0510dd92-0910-4bfd-a419-e8a2ddee3105.png)

![Validation controls in ASP.Net](https://www.mindstick.com/mindstickarticle/3af3cb8e-3bc0-4fc1-a2cd-8ce971c65ef2/images/b4265766-59dc-4348-bf5f-d11c8c27b36d.png)\

##### ValidationSummary

The ValidationSummary control is used to display a summary of all validation errors occurred in a Web page. The error message displayed in this control is specified by the ErrorMessage property of each validation control. If the ErrorMessage property of the validation control is not set, no error message is displayed for that validation control.

***You can also read these post***

[https://www.mindstick.com/Blog/768/validation-controls-in-asp-dot-net](https://www.mindstick.com/Blog/768/validation-controls-in-asp-dot-net)

---

Original Source: https://www.mindstick.com/articles/43/validation-controls-in-asp-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
