The
Validation controls are used for validating the data entered in Text Box(input) controls. When any user enters the data on a text Box (web page) and
submits the page on the server ,then validation controls are used to check the
data field entered by the user .if any data entered by the
user is invalid (not correct format) then validation control displays
an error message on the screen. All the error message is defined as
properties values of validation controls. All the validation controls comes
under System.UI.Webcontrols Namespace.
There are some validation controls ,that are used in ASP.NET.Which are given below :
There are some validation controls ,that are used in ASP.NET.Which are given below :
1.
The RequiredField Validator control
2.
The RangeValidator control
3.
The RegularExpressionValidator control
4.
The CompareValidator control
5.
The customValidator control
1. ) The
RequiredFieldValidator control:-
It is
used to check that there must be some value specify within the control.
Properties:- Some important properties are given below:-
·
ControlToValidate:-
It is used to set the control field (text box) for validation.
·
Initial value:-
It is only guidance for user which display default in the
control.This property can be used for drop down list.
·
Text :- It is used to set the text value
(Name) to the validation control.
2. ) The RangeValidator control:-
It checks
whether or not the value of an input control is inside a specified range of
values.This controls can be used with mobile Number,Age,Date of Birth etc. on
the websites. I will discuss later this with the help of a real
time example.
There are some important properties of RangeValidator control which are given below:-
There are some important properties of RangeValidator control which are given below:-
·
ControlToValidate:-
It is used to set the specific control to validate.
·
Minimum Value:-
It is used to hold the minimum value of the valid range.
·
Maximum value:-It
holds the maximum value of the valid range.
·
Type:- You can set Type
properties also after the above properties if required.
String --> For a string data
type Integer --> For a Integer Data type
Double:-->
For A double data type currency --> For currency
data type Date --> For a date data type
3. ) The CompareValidator control:-
It is used to compare the values of two fields whether it is same or not.If both controls values are not same then it will show error message.I will implement this control below with one real example.
There are some operator property also that is used for different type of comparisons.
It is used to compare the values of two fields whether it is same or not.If both controls values are not same then it will show error message.I will implement this control below with one real example.
There are some operator property also that is used for different type of comparisons.
·
Equal:-It
is used to check whether the compared values are equal.
·
Not Equal :- It is used to check that control
are not equal to each other.
·
Greater than --> It is used for greater than
relationship.
·
GreaterThanEqual:-
It is for GreaterThanEqual relationship.
·
LessThan-->
It is for less than relationship.
·
LessThanEqual:- It
is for less than equal relationship.
4. ) The RegularExpressionValidation control:-
It is use to check whether or not the text matches a certain pattern.There are some elements that used to make expression,which are given below:-
\d --> [0->9] .it takes value zero to nine.
\D--> Other than [0->9].
\w --> [a->z][A->Z][0->9]
\s --> space
\S--> other value except space.
{Length}-->{min ,max}
[ ] --> choice of given character(one out of them).
( ) --> group of validator
| --> OR
We can use special characters which are given below:
* --> 0 -> more numeric values
+ --> 1 --> more numeric values
? --> 0(min) or 1 (max)
Note:- This is used to specify the preceded character,how many time used.
5. ) The CustomValidator control:-
This validation control is used to customize and implement data validation according to our condition and requirement.
Ex.
Suppose, if we want to check given number is even or odd then we can not use our existing controls.so that ,to solve this type of problem we can use customvalidation control in ASP.NET.
There are some properties of custom validator controls which are given below:-
It is use to check whether or not the text matches a certain pattern.There are some elements that used to make expression,which are given below:-
\d --> [0->9] .it takes value zero to nine.
\D--> Other than [0->9].
\w --> [a->z][A->Z][0->9]
\s --> space
\S--> other value except space.
{Length}-->{min ,max}
[ ] --> choice of given character(one out of them).
( ) --> group of validator
| --> OR
We can use special characters which are given below:
* --> 0 -> more numeric values
+ --> 1 --> more numeric values
? --> 0(min) or 1 (max)
Note:- This is used to specify the preceded character,how many time used.
5. ) The CustomValidator control:-
This validation control is used to customize and implement data validation according to our condition and requirement.
Ex.
Suppose, if we want to check given number is even or odd then we can not use our existing controls.so that ,to solve this type of problem we can use customvalidation control in ASP.NET.
There are some properties of custom validator controls which are given below:-
·
ValidateEmptyText -->It is used to
set a Boolean value indicating whether empty text should be validated or not.
·
ClientValidationFunction:->It
is used to set the name of custom client -side script function used for
validation.
There are some other important property that can be used for
validation.Which are given below:-
· "IsValid"
property of page class:- If all the validations controls are IsValid property will be true,page class
property also contains true.But if only one validation control IsValid property is false then page class
property will also contains false.
Ex.
Button click.....................
{
if(Page.IsValid)
{
Label1.text="No Error";
}
else
{
Label1.text="Error";
}
}
6.
) The Validation Summary control:-
This control is mostly less used.It is used to collect all the validation control error messages and display it collectively on the screen. There are some important property of validation summary control
·
DisplayMode:-
It is used to set the display mode of the validation summary control.
·
Forecolor:-
It is used to set the foreground color of the control.
·
HeaderText:-
It is used to set the header text displayed at the top of the summary.
|
|
-----------------------------------------------Source Code----------------------------------
<body><form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Is Valid" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
<table class="style1">
<tr>
<td class="style2">
<asp:Label ID="Label2" runat="server" Text="User Name"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1" Display="Dynamic"
ErrorMessage="Plz Enter New User" ValidationGroup="g">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator6" runat="server"
ControlToValidate="TextBox1" Display="Dynamic"
ErrorMessage="Enter Correct Format"
ValidationExpression="[a-zA-Z\.\'\-_\s]{1,40}" ValidationGroup="g"></asp:RegularExpressionValidator>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="Label3" runat="server" Text="New Password"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="TextBox2" Display="Dynamic"
ErrorMessage="Plz Enter New Passward" ValidationGroup="g">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="TextBox2" ValidationGroup="g"></asp:RegularExpressionValidator>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="Label4" runat="server" Text="ReTpye Password"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox3" runat="server" TextMode="Password"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToCompare="TextBox2" ControlToValidate="TextBox3" Display="Dynamic"
ErrorMessage="Both Passward Not Same" ValidationGroup="g"></asp:CompareValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="TextBox3" Display="Dynamic"
ErrorMessage="Plz Enter New Retpye Passawrd" ValidationGroup="g">*</asp:RequiredFieldValidator>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="Label5" runat="server" Text="Mobile No"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
ControlToValidate="TextBox4" Display="Dynamic"
ErrorMessage="Plz Enter Mobile No" SetFocusOnError="True"
ValidationGroup="g">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server"
ControlToValidate="TextBox4" Display="Dynamic"
ErrorMessage="Plz Enter Mobile No Correct Format" SetFocusOnError="True"
ValidationExpression="^[987]+\d{9}$" ValidationGroup="g"></asp:RegularExpressionValidator>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="Label6" runat="server" Text="Email Id"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
ControlToValidate="TextBox5" Display="Dynamic"
ErrorMessage="PLZ ENTER Email Id" ValidationGroup="g">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server"
ControlToValidate="TextBox5" Display="Dynamic" ErrorMessage=" Not Match "
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
ValidationGroup="g"></asp:RegularExpressionValidator>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="Label7" runat="server" Text="Website"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server"
ControlToValidate="TextBox6" Display="Dynamic"
ErrorMessage="Plz Enter Website " ValidationGroup="g">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator5" runat="server"
ControlToValidate="TextBox6" Display="Dynamic"
ErrorMessage="Plz Enter Web Site Correct Format"
ValidationExpression="([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?"
ValidationGroup="g"></asp:RegularExpressionValidator>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="Label8" runat="server" Text="Age"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server"
ControlToValidate="TextBox7" Display="Dynamic"
ErrorMessage="Plz Enter Age " ValidationGroup="g">*</asp:RequiredFieldValidator>
<asp:RangeValidator ID="RangeValidator1" runat="server"
ControlToValidate="TextBox7" Display="Dynamic"
ErrorMessage="Between 18 To 30" MaximumValue="30" MinimumValue="18"
SetFocusOnError="True" Type="Integer" ValidationGroup="g"></asp:RangeValidator>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="Label9" runat="server" Text="New Email Id"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox8" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server"
ControlToValidate="TextBox8" Display="Dynamic"
ErrorMessage="plz enter Email Id" ValidationGroup="g">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator7" runat="server"
ControlToValidate="TextBox8" Display="Dynamic"
ErrorMessage="plz enter Valid Email Id"
ValidationExpression="^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$"
ValidationGroup="g"></asp:RegularExpressionValidator>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td class="style2">
<asp:Button ID="Button2" runat="server" Text="Validation" ValidationGroup="g" />
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
</table>
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
BackColor="#FF33CC" BorderColor="#FFCCFF" DisplayMode="List"
ShowMessageBox="True" ValidationGroup="g" />
</form>
</body>
---------aspx code ----------
protected void Button1_Click(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
Label1.Text = "No Errror";
}
else
{
Label1.Text = "No Errror";
}
}
No comments:
Post a Comment