Total Pageviews

17 April 2018

What are the different type of validators in asp.net

Different Type of Validators ?

There are following types of validators in asp.net are given as below-

1.Required Field Validator
2.Range Validator
3.Compare Validator
4.Custom Validator
5.Regular Expression Validator
6.Summary Validator



1.Required Field Validator

In required validator use for input type of text compulsory to fill otherwise show error.

for example

<form id="form1" runat="server">
    Your name:<br />
    <asp:TextBox runat="server" id="t1" />
    <asp:RequiredFieldValidator runat="server" id="reqName" controltovalidate="t1" errormessage="Please enter your name!" />
    <br /><br />
    <asp:Button runat="server" id="btnSubmitForm" text="ok" />
</form>

2.Range Validator

In range validator define specific range to give the value in text box.
For Example

Date:<br />
<asp:TextBox runat="server" id="txtDate" />
<asp:RangeValidator runat="server" id="rngDate" controltovalidate="txtDate" type="Date" minimumvalue="01-01-2006" maximumvalue="31-12-2006" errormessage="Please enter a valid date within 2006!" />

<br /><br />


3.Compare Validator

In compare validator compare two textbox value.It just like a required field validator.
For example if one textbox contain 2 value and other contain 3 value

Small number:<br />
<asp:TextBox runat="server" id="txtSmallNumber" /><br /><br />
Big number:<br />
<asp:TextBox runat="server" id="txtBigNumber" /><br />
<asp:CompareValidator runat="server" id="cmpNumbers" controltovalidate="txtSmallNumber" controltocompare="txtBigNumber" operator="LessThan" type="Integer" errormessage="The first number should be smaller than the second number!" /><br />


4.Custom Validator

According to requirement create a custom validation which is validate by server side.


Custom text:<br />
<asp:TextBox runat="server" id="txtCustom" />
<asp:CustomValidator runat="server" id="cusCustom" controltovalidate="txtCustom" onservervalidate="cusCustom_ServerValidate" errormessage="The text must be exactly 8 characters long!" />
<br /><br />

Server side create a validation

protected void cusCustom_ServerValidate(object sender, ServerValidateEventArgs e)
{
    if(e.Value.Length == 8)
        e.IsValid = true;
    else
        e.IsValid = false;
}

5.Regular Expression

In regular expression contain expression code to check textbox value by the validation expression
for example
we define only integer value 1 to 4 digit.


digit number:<br />
<asp:TextBox runat="server" id="txtNumber" />
<asp:RegularExpressionValidator runat="server" id="rexNumber" controltovalidate="txtNumber" validationexpression="^[0-9]{4}$" errormessage="Please enter a 4 digit number!" />
<br /><br />

6.Summary of Validator
Summary of validation is allow to display all error when click submit button.


For example
<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">
    protected void Button_Submit_Click(object sender, System.EventArgs e)
    {
        Label_Message.Text = "Your submitted info....<br />" +
            "Name : " +
            TextBox_FirstName.Text.ToString() + " " +
            TextBox_LastName.Text.ToString() +
            "<br />City: " +
            TextBox_City.Text.ToString();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How to use ValidationSummary control in asp.net</title>
</head>
<body style="padding:25px">
    <form id="form1" runat="server">
    <div>
        <h2 style="color:MidnightBlue; font-style:italic;">     
            How to use ValidationSummary control
        </h2>     
        <hr width="450" align="left" color="Gainsboro" />
        <asp:Label
            ID="Label_Message"
            runat="server"
            Font-Bold="true"
            Font-Names="Comic Sans MS"
            ForeColor="OliveDrab"
            Font-Italic="true"
            Font-Size="X-Large"
            />
        <asp:ValidationSummary
            ID="ValidationSummary1"
            runat="server"
            HeaderText="Following error occurs....."
            ShowMessageBox="false"
            DisplayMode="BulletList"
            ShowSummary="true"
            BackColor="Snow"
            Width="450"
            ForeColor="Red"
            Font-Size="X-Large"
            Font-Italic="true"
            />
        <br /><br />
        <asp:Label
            ID="Label_FirstName"
            runat="server"
            Text="First name"
            AssociatedControlID="TextBox_FirstName"
            Font-Bold="true"
            Font-Size="Large"
            ForeColor="Navy"
            />
        <asp:TextBox
            ID="TextBox_FirstName"
            runat="server"
            Font-Bold="true"
            Font-Size="Large"
            Height="30"
            BackColor="Gold"
            Font-Names="Courier New"
            />
        <asp:RequiredFieldValidator
             ID="RequiredFieldValidator1"
             runat="server"
             ControlToValidate="TextBox_FirstName"
             ErrorMessage='Input your first name.'
             EnableClientScript="true"
             SetFocusOnError="true"
             Text="*"
             >
        </asp:RequiredFieldValidator>
        <br /><br />

        <asp:Label
            ID="Label_LastName"
            runat="server"
            Text="Last name"
            AssociatedControlID="TextBox_LastName"
            Font-Bold="true"
            Font-Size="Large"
            ForeColor="Navy"
            />
        <asp:TextBox
            ID="TextBox_LastName"
            runat="server"
            Font-Bold="true"
            Font-Size="Large"
            Height="30"
            BackColor="Gold"
            Font-Names="Courier New"
            />
        <asp:RequiredFieldValidator
             ID="RequiredFieldValidator2"
             runat="server"
             ControlToValidate="TextBox_LastName"
             ErrorMessage='Input your last name.'
             EnableClientScript="true"
             SetFocusOnError="true"
             Text="*"
             >
        </asp:RequiredFieldValidator>
        <br /><br />
       
        <asp:Label
            ID="Label_City"
            runat="server"
            Text="City"
            AssociatedControlID="TextBox_City"
            Font-Bold="true"
            Font-Size="Large"
            ForeColor="Navy"
            />
        <asp:TextBox
            ID="TextBox_City"
            runat="server"
            Font-Bold="true"
            Font-Size="Large"
            Height="30"
            BackColor="Gold"
            Font-Names="Courier New"
            />
        <asp:RequiredFieldValidator
             ID="RequiredFieldValidator3"
             runat="server"
             ControlToValidate="TextBox_City"
             ErrorMessage='Input your city.'
             EnableClientScript="true"
             SetFocusOnError="true"
             Text="*"
             >
        </asp:RequiredFieldValidator>
        <br /><br />
        <asp:Button
            ID="Button_Submit"
            runat="server"
            Text="Submit"
            OnClick="Button_Submit_Click"
            Font-Bold="true"
            Font-Size="Large"
            ForeColor="DodgerBlue"
            Font-Names="Monaco"
            Height="45"
            Width="350"
            />
        <br /><br />
    </div>
    </form>
</body>
</html>

I hope my post is usefull for you............


No comments:

Post a Comment

Contact Form

Name

Email *

Message *