Validation Controls in Asp.Net


Validating user input is a common scenario in a Web-based application. For production applications, developers often end up spending a lot more time and code on this task than we would like. In building the ASP.NET page framework, it was important to try and make the task of validating input a lot easer than it has been in the past.

VALIDATION CONTROLS


The Validation is used to validate an input data.

TYPES OF VALIDATION


There are different types of Validator . They are:
  • Required Field Validator

  • Range Validator

  • Compare Validator

  • Regular Expression Validator

  • Custom Validator

  • Validator Summary


  • Required Field Validator


    It make an input Control as a required Field.

    Example



    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
    ErrorMessage="Value Required" Text="*"></asp:RequiredFieldValidator>
    <asp:Button ID="Button1" runat="server" Text="Click Me" />


    Range Validator


    It Checks the user input is between Two value.It is possible to check ranges within numbers, dates, and characters

    Examples



    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="TextBox1"
    MinimumValue="1" MaximumValue="100" ErrorMessage="Enter 1 to 100" Type="Integer" Text="*"></asp:RangeValidator>
    <asp:Button ID="Button1" runat="server" Text="Click Me" />


    Compare Validator


    It is used to compare one input value to the another input value

    Examples



    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="Sno and Emp No are Different"
    ControlToCompare="TextBox1" ControlToValidate="TextBox1" Text="*"></asp:CompareValidator>
    <asp:Button ID="Button1" runat="server" Text="Click Me" />


    Regular Expression validator


    It Refers the input values refer the special pattern

    Example


     
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Email is not valid"
    ControlToValidate="TextBox3" ValidationExpression="^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$" Text="*"></asp:RegularExpressionValidator>
    <asp:Button ID="Button1" runat="server" Text="Click Me" />


    Custom validator


    It allows you to write a method to handle the validation of the value entered.

    Example


     
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="TextBox4"
    OnServerValidate="username" ErrorMessage="Enter 2 to 9 letter" Text="*"></asp:CustomValidator>
    <asp:Button ID="Button1" runat="server" Text="Click Me" />




    <script language ="vbscript" runat="server">
    Sub username(ByVal source As Object, ByVal args As ServerValidateEventArgs)
    If Len(args.Value) < 2 Or Len(args.Value) > 9 Then
    args.IsValid = False
    Else
    args.IsValid = True
    End If
    End Sub

    </script>



    validation Summary


    It is used to display a summary of all validation errors occurred in a Web page.

    Example:



    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>

    <script language ="vbscript" runat="server">
    Sub username(ByVal source As Object, ByVal args As ServerValidateEventArgs)
    If Len(args.Value) < 2 Or Len(args.Value) > 9 Then
    args.IsValid = False
    Else
    args.IsValid = True
    End If
    End Sub

    </script>

    </head>
    <body>
    <form id="form1" runat="server">
    <table width="100%" cellpadding="0" cellspacing="0">
    <tr>
    <td>
    <asp:Label ID="Label1" runat="server" Text="Sno"></asp:Label>
    </td>
    <td>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
    ErrorMessage="Value Required" Text="*"></asp:RequiredFieldValidator>
    <asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="TextBox1"
    MinimumValue="1" MaximumValue="100" ErrorMessage="Enter 1 to 100" Type="Integer" Text="*"></asp:RangeValidator>
    </td>
    </tr>
    <tr>
    <td>
    <asp:Label ID="Label2" runat="server" Text="Emp No"></asp:Label>
    </td>
    <td>
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    <asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="Sno and Emp No are Different"
    ControlToCompare="TextBox1" ControlToValidate="TextBox2" Text="*"></asp:CompareValidator>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox2"
    ErrorMessage="Value Required" Text="*"></asp:RequiredFieldValidator>
    </td>
    </tr>
    <tr>
    <td>
    <asp:Label ID="Label4" runat="server" Text="User Name"></asp:Label>
    </td>
    <td>
    <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
    <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="TextBox4"
    OnServerValidate="username" ErrorMessage="Enter 2 to 9 letter" Text="*"></asp:CustomValidator>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="TextBox4"
    ErrorMessage="Value Required" Text="*"></asp:RequiredFieldValidator>
    </td>
    </tr>
    <tr>
    <td>
    <asp:Label ID="Label5" runat="server" Text="Password"></asp:Label>
    </td>
    <td>
    <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
    <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ErrorMessage="ENTER 8 T0 10 CHARACTER WITH ALPHANUMERIC CHATRACTER WITH SPECIAL CHARACTER "
    ControlToValidate="TextBox5" ValidationExpression="(?=^.{8,12}$)(?=.*\d)(?=.*\W+)(?![.\n]).*$" Text="*"></asp:RegularExpressionValidator>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="TextBox5"
    ErrorMessage="Value Required" Text="*"></asp:RequiredFieldValidator>
    </td>
    </tr>
    <tr>
    <td>
    <asp:Label ID="Label3" runat="server" Text="Email"></asp:Label>
    </td>
    <td>
    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Email is not valid"
    ControlToValidate="TextBox3" ValidationExpression="^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$" Text="*"></asp:RegularExpressionValidator>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="TextBox3"
    ErrorMessage="Value Required" Text="*"></asp:RequiredFieldValidator>
    </td>
    </tr>
    <tr>
    <td>
    <asp:Button ID="Button1" runat="server" Text="Click Me" />
    </td>
    </tr>
    <tr>
    <asp:ValidationSummary ID="ValidationSummary1" runat="server" HeaderText="You must enter a value in the following fields:" DisplayMode ="BulletList" EnableClientScript ="true" />
    </tr>
    </table>
    </form>
    </body>
    </html>



    Reference: Validation Controls in Asp.Net


    Comments

    No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: