articles

Home / DeveloperSection / Articles / Validation controls in ASP.Net

Validation controls in ASP.Net

Anonymous User8443 21-Jul-2010

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-index1left64pxtop88pxpositionabsolute" TabIndex="3"></asp:TextBox>
    <asp:TextBox ID="TextBox1" runat="server"
        style="z-index1left63pxtop51pxpositionabsolute" TabIndex="1"></asp:TextBox>
   
    <asp:Button ID="Button1" runat="server"
        style="z-index1left68pxtop122pxpositionabsolute"
        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>   
    </form>
Screen shot

 Validation controls in ASP.Net

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-index1left60pxtop55pxpositionabsolute"></asp:TextBox>
    <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"
        style="position:absolutetop159pxleft60pxwidth209px;"
        ControlToValidate="TextBox1" MaximumValue="50" MinimumValue="20" Type="Integer"></asp:RangeValidator>
    </form>

 

Screen shot

 Validation controls in ASP.Net

Validation controls in ASP.Net

 

 

 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-index1left101pxtop45pxpositionabsolutewidth197px;"></asp:TextBox>
    <asp:Label ID="Label1" runat="server" Text="E-mail"
        style="position:absolutetop48pxleft32pxwidth92px;"></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:absolutetop120pxleft31pxwidth251px;"
        ControlToValidate="TextBox1"
        ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
    <asp:Button ID="Button1" runat="server"
        style="z-index1left113pxtop83pxpositionabsolute" Text="Button" />
    </form>
Screen shot

 Validation controls in ASP.Net

Validation controls in ASP.Net

 

 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-index1left30pxtop100pxpositionabsolute"
        TabIndex="3"></asp:TextBox>
    <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"
        style="position:absolutetop190pxleft25pxwidth228px;"></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:absolutetop53pxleft200pxwidth223px;"
        ControlToValidate="TextBox1" Operator="LessThanEqual" ValueToCompare="50"
        Type="Integer"></asp:CompareValidator>
    </form>
Screen Shot

 Validation controls in ASP.Net

Validation controls in ASP.Net

 

 

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-index1left114pxtop48pxpositionabsolute"></asp:TextBox>
    <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>   
    </form>
//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;
            else
                args.IsValid = false;
        } 
Screen shot

 

 Validation controls in ASP.Net

Validation controls in ASP.Net

 

 

 

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

 

 


Updated 07-Sep-2019
I am a content writter !

Leave Comment

Comments

Liked By