ASP.NET

Validation Controls Examples – RequiredFieldValidator, CompareValidator, and RangeValidator

Programmingempire

To begin with, in this post, I will provide certain Validation Controls Examples – RequiredFieldValidator, CompareValidator, and RangeValidator. In my earlier post on validation controls, I have explained the concept and here I will demonstrate them using code examples.

Example of Required Field Validator Control

The following example shows how to use the Required Field Validator control. Basically, the ControlToValidate property specifies which control we are validating. Also, the Text property displays the text message in case the corresponding field is left blank.

Enter Some Text:  *<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
            <asp:RequiredFieldValidator ID="r1" runat="server" 
                ErrorMessage="The Field Can't be Left Blank"
                 ControlToValidate="TextBox1"
                 BackColor="LightYellow"
                 Display="Static"
                 Text="This Field is Mandatory!"
        ></asp:RequiredFieldValidator>

Example of Compare Validator Control

Since the CompareValidator control performs a comparison on the content of two specific controls, it has two properties for this purpose – ControlToValidate and ControlToCompare. The following CompareValidator control compares the content of TextBox2 and TextBox3 for Equality as shown in the Operator property.

Comparing the Content of Two Fields<br />
            Enter Text: <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
            Enter Text Again: <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
            <asp:CompareValidator ID="c1" runat="server" 
            ErrorMessage="Two Values don't match"
                 BackColor="LightBlue"
                 Display="Static"
                 Operator="Equal"
                 Type="String"
                 Text="Mismatch!"
                 ControlToValidate="TextBox2"
                 ControlToCompare="TextBox3"
    ></asp:CompareValidator>

Example of Range Validator Control

For the purpose of getting input in a specified range, we use the RangeValidator control. In other words, this control checks whether the input value falls in the specified range or not. The following example shows how to use RangeValidator control to ensure that the user enters a value in the range [1-100] in a TextBox. Besides the ControlToValidate property, we need to specify MinimumValue, MaximumValue, and the Type properties.

Validating Range
            Enter a number [10-100]: <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br />
            <asp:RangeValidator ID="r2" runat="server" 
            ErrorMessage="Invalid Range!"
             Text="Value Not in Given Range!"
                 BackColor="LightCoral"
                 Display="Static"
                 MaximumValue="100"
                 MinimumValue="10"
                 Type="Integer"
                 ControlToValidate="TextBox4"
         ></asp:RangeValidator>

The Complete Code

Now that, we use the above-mentioned three validation controls to perform the required validation. the output shows the text message that we have specified in the Text property of the respective control. Whenever the input in the corresponding TextBox is not valid, the text message is displayed.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ValidationControlExamples.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
           Enter Some Text:  *<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
            <asp:RequiredFieldValidator ID="r1" runat="server" 
                ErrorMessage="The Field Can't be Left Blank"
                 ControlToValidate="TextBox1"
                 BackColor="LightYellow"
                 Display="Static"
                 Text="This Field is Mandatory!"
                 ></asp:RequiredFieldValidator>

            <hr />
            Comparing the Content of Two Fields<br />
            Enter Text: <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
            Enter Text Again: <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
            <asp:CompareValidator ID="c1" runat="server" 
            ErrorMessage="Two Values don't match"
                 BackColor="LightBlue"
                 Display="Static"
                 Operator="Equal"
                 Type="String"
                 Text="Mismatch!"
                 ControlToValidate="TextBox2"
                 ControlToCompare="TextBox3"
                 ></asp:CompareValidator>
             <hr />

            Validating Range
            Enter a number [10-100]: <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br />
            <asp:RangeValidator ID="r2" runat="server" 
            ErrorMessage="Invalid Range!"
             Text="Value Not in Given Range!"
                 BackColor="LightCoral"
                 Display="Static"
                 MaximumValue="100"
                 MinimumValue="10"
                 Type="Integer"
                 ControlToValidate="TextBox4"
          ></asp:RangeValidator>
        </div>

    </form>
</body>
</html>

Output

Validation Controls Examples
Validation Controls Examples

Summary

To sum up, this article shows the example of the usage of validation controls in an ASP.NET application. Evidently, using server-side validation controls in ASP.NET is quite simple and straightforward. Further, you don’t need to write much code for validation for most of these controls. Therefore, the above example uses only the ASPX file containing the markup. However, if we need to provide a custom validation control then we need to define the appropriate method also. Another example of validation control that uses a custom validator is shown here.


Further Reading

Parameter and ParameterCollection in ADO.NET

Database Manipulation Using DataGrid

Example of Button and Link Button Control in ASP.NET

Example of Chart Control in ASP.NET

Creating a DataTable from a DataReader in ASP.NET

Deleting a Record using DataGrid Control in ASP.NET

Edit a Record Using DataGrid Control in ASP.NET

Insert a Record Using ItemCommand Event in DataGrid

CRUD Operations with DataGrid in ASP.NET

Creating Columns in a DataGrid Control

XML Documents and DataSet in ASP.NET

Code Render Block in ASP.NET

ASP.NET Core Features and Advantages

Display Images Using DataList Control

Adding Images Using Image Control

Creating a Group of Radio Buttons Using RadioButtonList Control

Example of Button Control in ASP.NET

Using MD5 Hash Algorithm

ItemDataBound Event in DataList

More Features of DataList in ASP.NET

A Simple Example of Using a DataList Control in ASP.NET

Properties and Methods of DataList Control in ASP.NET

ASP.NET Practice Exercise

Exploring DataList Control in ASP.NET

Custom Validator Control in ASP.NET

Validation Summary Control in ASP.NET

Validation Controls Examples – RequiredFieldValidator, CompareValidator, and RangeValidator

An Example of Data Binding with RadioButtonList Control

Binding Data to Web Control in ADO.NET

Examples of AdRotator Control in ASP.NET

Examples of Validation Controls in ASP.NET

Overview of MVC architecture in ASP.NET

Related Topics

programmingempire

You may also like...