Understanding ASP.NET Validation Controls
Validation Controls:
A Validation server control is used to validate the data of
an input control; So that check withers input is correct or proper, if input
is not correct or proper than prompts message display to client.
2) RangeValidator
3) Regular Expression Validator
4) Compare Validator
5) Custom Validator
6)ValidationSummary
General
properties:
|
General
properties:
·
ControlToValidate="txtfname"
·
ErrorMessage="RequiredFieldValidator"
For Example:-
Source Code like this
First
Name:<asp:TextBox ID="txtfname"
runat="server"
ValidationGroup="name_group">
</asp:TextBox>
 <asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server"
ControlToValidate="txtfname"
ErrorMessage="You
must enter your First Name">*</asp:RequiredFieldValidator>
|
2) ![]()
General
properties:
·
ControlToValidate="txtage"
·
ErrorMessage="You must be between 25 and 35"
·
MaximumValue="35"
·
MinimumValue="25"
·
Type=”Integer/Double/Date/Currency ">
Source Code like this
Age:<asp:TextBox ID="txtage" runat="server"></asp:TextBox>
<asp:RangeValidator ID="RangeValidator1"
runat="server"
ControlToValidate="txtage" ErrorMessage="You must be between 25 and 35"
MaximumValue="35"
MinimumValue="25"
Type="Integer">
</asp:RangeValidator>
OR
Last Name:<asp:TextBox ID="txtage" runat="server"></asp:TextBox>
<asp:RangeValidator ID="RangeValidator3"
runat="server"
ControlToValidate="txtage"
ErrorMessage="You
must be between A and D"
MaximumValue="D"
MinimumValue="A"
Type="String">
</asp:RangeValidator>
Similar for Double,Date,Currency
|
Note:-Type:- there
are 5 type as following (Interview
Question)
|
3) ![]() : You can check the validation of the commonly performed formats such as e-mail addresses, telephone numbers, social security numbers, and postal code for different countries. General properties:
·
ControlToValidate=" txtemail
·
ErrorMessage=" You must enter an
email address (abc@google.com)" or ErrorMessage='<img
src="Image/error.gif" />'
Source Code like this
Email: <asp:TextBox ID="txtemail"
runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="txtemail"
ErrorMessage=" You must enter an
email address (abc@google.com)"
</asp:RegularExpressionValidator>
Or
Also display error.gif image instade of message(You must enter an email
address (abc@google.com))
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
ControlToValidate="txtalteremail"
ErrorMessage='<img
src="Image/error.gif" />'
ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
|
error
messages can be displayed in a list, bulleted list, or paragraph.
General
properties:
·
HeaderText="You received the following errors:"
DisplayMode property :- you
can change the value of the DisplayMode property
of the Validation Summary control. The possible values of this control can be
set to the following:
A dialog box showing the page's
validation errors
<asp:ValidationSummary ID="ValidationSummary1"
runat="server" HeaderText="You received the following errors:"
ShowMessageBox=true(You must type in source code than &
than display dialog box) />
Using
different text for the validation error messages
<asp:ValidationSummary ID="ValidationSummary1"
runat="server" HeaderText="You received the following errors:" />
Source Code like this
<asp:ValidationSummary ID="ValidationSummary1"
runat="server" HeaderText="You received the following errors:" />
|
Step-I Create ValidationControlExamples.aspx as following |
Step-II
Source Code like this
Take table 20*3
Then take all control
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled
Page</title>
<style type="text/css">
.style1
{
width:
63%;
background-color:
#C0C0C0;
height:
619px;
}
.style2
{
width:
121px;
}
.style3
{
width:
80px;
}
.style4
{
width:
324px;
}
.style5
{
width:
121px;
height:
21px;
}
.style6
{
width: 80px;
height:
21px;
}
.style7
{
width:
324px;
height:
21px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="style1">
<tr>
<td class="style2">
</td>
<td class="style3">
</td>
<td class="style4">
</td>
</tr>
<tr>
<td class="style2">
FirstName</td>
<td class="style3">
<asp:TextBox ID="txtfname" runat="server"></asp:TextBox>
</td>
<td class="style4">
<asp:RequiredFieldValidator
ID="RequiredFieldValidator1"
runat="server"
ControlToValidate="txtfname" ErrorMessage="You must enter your First Name">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td class="style3">
</td>
<td class="style4">
</td>
</tr>
<tr>
<td class="style5">
Last name:
</td>
<td class="style6">
<asp:TextBox id="txtlname"
runat="server"></asp:TextBox>
</td>
<td class="style7">
<asp:RangeValidator id="RangeValidator3"
runat="server"
ControlToValidate="txtlname"
ErrorMessage="Your last name needs to be between A and D"
MaximumValue="D" MinimumValue="A"></asp:RangeValidator>
</td> </tr> <tr>
<td class="style2">
</td>
<td class="style3">
</td>
<td class="style4">
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
ControlToValidate="txtlname" ErrorMessage="RequiredFieldValidator_Lname">
</asp:RequiredFieldValidator>
</td></tr><tr>
<td class="style2">
</td>
<td class="style3">
</td>
<td class="style4">
</td>
</tr> <tr>
<td class="style2">
Age:
</td>
<td class="style3">
<asp:TextBox ID="txtage" runat="server"></asp:TextBox>
</td>
<td class="style4">
<asp:RangeValidator ID="RangeValidator1" runat="server"
ControlToValidate="txtage" ErrorMessage="You must be between 25 and 35"
MaximumValue="35" MinimumValue="25" Type="Integer">
</asp:RangeValidator>
</td></tr> <tr>
<td class="style2">
</td>
<td class="style3">
</td>
<td class="style4">
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtage" ErrorMessage="RequiredFieldValidator_Age">
</asp:RequiredFieldValidator>
</td></tr><tr>
<td class="style2">
</td>
<td class="style3">
</td>
<td class="style4">
</td>
</tr>
<tr>
<td class="style2">
Email:</td>
<td class="style3">
<asp:TextBox ID="txtemail" runat="server"></asp:TextBox>
</td>
<td class="style4">
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
runat="server"
ControlToValidate="txtemail"
ErrorMessage="You must
enter an email address (abc@google.com)"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
</asp:RegularExpressionValidator>
</td> </tr><tr>
<td class="style2">
</td>
<td class="style3">
</td>
<td class="style4">
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
ControlToValidate="txtemail"
rrorMessage="RequiredFieldValidator_Email">
</asp:RequiredFieldValidator>
</td> </tr><tr>
<td class="style2">
</td>
<td class="style3">
</td>
<td class="style4">
</td>
</tr>
<tr>
<td class="style2">
Alternative
Email:</td>
<td class="style3">
<asp:TextBox ID="txtalteremail" runat="server"></asp:TextBox>
</td>
<td class="style4">
<asp:RegularExpressionValidator ID="RegularExpressionValidator2"
runat="server"
ControlToValidate="txtalteremail"
ErrorMessage='<img src="Image/error.gif" />'
ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
</asp:RegularExpressionValidator>
<br />
<asp:RequiredFieldValidator
ID="RequiredFieldValidator6"
runat="server"
ControlToValidate="txtalteremail" ErrorMessage="RequiredFieldValidator_AlternativeEmail">
</asp:RequiredFieldValidator>
</td></tr><tr>
<td class="style2">
</td>
<td class="style3">
</td>
<td class="style4">
</td>
</tr>
<tr>
<td class="style2">
Password</td>
<td class="style3">
<asp:TextBox ID="txtpass"
runat="server"
TextMode="Password"></asp:TextBox>
</td>
<td class="style4">
<asp:CompareValidator ID="CompareValidator1" runat="server"
ErrorMessage="Passwords do not match!!!" ControlToCompare="txtpass"
ControlToValidate="txtconfirmpass"></asp:CompareValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server"
ControlToValidate="txtpass"
ErrorMessage="RequiredFieldValidator_Password"></asp:RequiredFieldValidator>
</td></tr><tr>
<td class="style2">
Confirm Password : <td class="style3">
<asp:TextBox ID="txtconfirmpass" runat="server" TextMode="Password"></asp:TextBox>
</td>
<td class="style4">
</td>
</tr>
<tr>
<td class="style2">
</td>
<td class="style3">
</td>
<td class="style4">
</td>
</tr>
<tr>
<td class="style2">
</td>
<td class="style3">
<asp:Button ID="Button1" runat="server" Text="Button" Width="121px" />
</td>
<td class="style4">
</td>
</tr>
<tr>
<td class="style2">
</td>
<td class="style3">
</td>
<td class="style4">
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
HeaderText="You
received the following errors:" ShowMessageBox=true />
</td>
</tr>
<tr>
<td class="style2">
</td>
<td class="style3">
</td>
<td class="style4">
</td></tr>
</table></div></form></body></html>
|
Step-III![]() |
How to clear all controls values in web page?
|
Using Clear Method to clear all control value, let’s
start
For an example we are create Employee Registration page in our project in that time we placed lots of controls like textbox, radio button ,drop down list & check box etc. In this Example I took two Concept(way),
First Way: -
If user click that clear button we need to clear all controls values, with using C Sharp.
Second Way:-If
user click that clear button we need to clear all controls values, with using Java script.
Client side
code
I have places some controls in client side of the web page (is called ClearAllMethod.aspx) and shown in below figure.
Way-I(USING CSHARP)
Using Clear Method to clear all control value, with
using C Sharp.
Server side
using System;
using System.Data;
public partial class ExampleOfInsertDisplay
: System.Web.UI.Page
{
protected void
Page_Load(object sender, EventArgs e)
{
}
protected void
btnresetc_Click(object sender, EventArgs e)
{
//clear method i call here
ClearAll();
}
//this is a clear method to clear all control
public void ClearAll()
{
//First WAY -I
//TextBox1.Text = "";
//RadioButton1.Checked = false;
//RadioButton2.Checked = false;
//TextBox2.Text = "";
//TextBox3.Text = "";
//DropDownList1.SelectedIndex = 0;
//CheckBox1.Checked = false;
//CheckBox2.Checked = false;
//CheckBox3.Checked = false;
//CheckBox4.Checked = false;
//Second WAY-II
foreach (Control
item in form1.Controls)
{
//Clear all textbox values
if (item is
TextBox)
((TextBox)item).Text
= "";
//Clear all radio buttons
if (item is
RadioButton)
((RadioButton)item).Checked
= false;
//clear all check boxes
if (item is
CheckBox)
((CheckBox)item).Checked
= false;
//Clear all radio buttons
if (item is
DropDownList)
((DropDownList)item).SelectedIndex
= 0;
}
}
}
OUTPUT:
Buil---àRun(F5)---àCHECK OUTPUT
When you click Reset Using C# btn all
control value is clear.
Way-II(USING
JavaScript)
Using Clear Method to clear all control value, with
using JavaScript.
I have write one JavaScript function to
get form elements and check that element type and assign values based on that
type.
Client side
<head runat="server">
<title>Untitled
Page</title>
<script type="text/javascript"
language="javascript"
>
function clearAllControlValue()
{
var
myfrm = document.forms[0];
for
(i = 0; i < myfrm.elements.length; i++)
{
if
(myfrm.elements[i].type == "checkbox")
{
myfrm.elements[i].checked = false;
}
if
(myfrm.elements[i].type == "radio")
{
myfrm.elements[i].checked = false;
}
if
(myfrm.elements[i].type == "text")
{
myfrm.elements[i].value="";
}
if
(myfrm.elements[i].type == "select")
{
myfrm.elements[i].value=0;
}
}
}
</script>
</head>
And then call in the
button click event(mean OnClientClick) like this
<asp:Button ID="btnresetjava"
runat="server"
OnClientClick="clearAllControlValue()"
Text="Reset Using Javascript"/>
OUTPUT:
Buil---àRun(F5)---àCHECK OUTPUT
When you click Reset Using JavaScript
btn all control value is clear.
|
How
to use Label? Properties For Label?
|
What is mean by AssociatedControlID?
AssociatedControlID,
it tells label where focus should go once the label is clicked.
See as
following example
Select
Lable(lblname)--àProperties--à1)Text=Enter
First Name 2)AssociatedControlID=txtfname (NAME OF TextBox)
Similar
for Lable(lbllastname)--àProperties--à1)Text=Enter Last
Name 2)AssociatedControlID=txtlname (NAME OF TextBox)
Then
Build--àRun(F5)
|
How
to use Panel Control?
|
Step-I
Create
a new aspx page(ExampleOf2Panel.aspx).
Then add two Panel & just take one button in Panel1 as
shown in below figure
Step-II
If I click submit button ,then I
display Panel2 , so that I want to set Panel2 --àProperties --àVisible="False"
.
OR
At
the time of page Load you have to set Panel2.Visible=False see below cs code
protected void Page_Load(object sender, EventArgs
e)
{
Panel2.Visible
= false;
}
WHEN I click submit button ,then
Panel2 will be display .
Then write code on Submit Button
protected void Button1_Click(object sender, EventArgs
e)
{
Panel1.Visible = false;
Panel2.Visible = true;
}
OutPut:-
Build----àRun(F5)
|