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


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


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

applied is left blank.

<form id="form1" runat="server">
    <asp:TextBox ID="TextBox2" runat="server"
        style="z-index1left64pxtop88pxpositionabsolute" TabIndex="3"></asp:TextBox>
    <asp:TextBox ID="TextBox1" runat="server"
        style="z-index1left63pxtop51pxpositionabsolute" TabIndex="1"></asp:TextBox>
    <asp:Button ID="Button1" runat="server"
        Text="Button" />
  //applying RequiredFieldValidator to TextBox1  
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
        ErrorMessage="* You must enter a value into textbox"
        style="position:absolutetop51pxleft220px;" 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:absolutetop87pxleft222px;" Display="Static">*</asp:RequiredFieldValidator>   
Screen shot



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


<form id="form1" runat="server">
    <asp:TextBox ID="TextBox1" runat="server"
    <asp:Button ID="Button1" runat="server"
        style="z-index1left96pxtop109pxpositionabsolute" 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"
        ControlToValidate="TextBox1" MaximumValue="50" MinimumValue="20" Type="Integer"></asp:RangeValidator>


Screen shot





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.

<form id="form1" runat="server">
    <asp:TextBox ID="TextBox1" runat="server"
    <asp:Label ID="Label1" runat="server" Text="E-mail"
//applying RegularExpressionValidator to TextBox1 to check for valid email address provided by the user.
    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Invalid Email ID"
    <asp:Button ID="Button1" runat="server"
        style="z-index1left113pxtop83pxpositionabsolute" Text="Button" />
Screen shot




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.

<form id="form1" runat="server">
    <asp:TextBox ID="TextBox2" runat="server"
    <asp:TextBox ID="TextBox1" runat="server"
        style="z-index1left30pxtop53pxpositionabsolute" TabIndex="1"></asp:TextBox>
    <asp:Button ID="Button1" runat="server"
        style="z-index1left58pxtop149pxpositionabsolute" 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"
//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"
        ControlToValidate="TextBox1" Operator="LessThanEqual" ValueToCompare="50"
Screen Shot





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



//code in .aspx file
<form id="form1" runat="server">
    <asp:TextBox ID="TextBox1" runat="server"
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
        style="z-index1left149pxtop95pxpositionabsolute" 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>   
//code in .cs file
protected 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;
                args.IsValid = false;
Screen shot







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




  Modified On Nov-30-2017 12:51:29 AM

Leave Comment