Hi Friend’s , Here I have explained validation in .NET.
Introduction:
Validation controls are hidden controls which are used to
validate the data, provided by the user at the client end. As we know that
sending a request to the server, it takes lot of time to respond. So it becomes
very costly to send all the data entered by the user at the client side, to the
server side for the validation. Validation controls work at the client side end
only if the browser support JavaScript and some DHTML and can check the date
before it sending it to the server.
You can assign more the one validation control to the same
data-entry- control, if you want to check multiple conditions at the same time.
There are six type of validation in .NET which is defined as below:-
1.) Required Field Validator:
This is the simplest control , which is used to ensure that
user has enter the data in data-control
or not. Means if user has left blank the field then it will show the error on
the page.
You can add this
control by simple drag and drop from the ToolBox control and simply double click on this control.
When you add RequiredFieldValidation control on your page , it add the following
code on your web page:-
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server"
ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
Example:
Validation.aspx:
<asp:TextBox ID="TextBox1"
runat="server"
style="z-index"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1"
ErrorMessage="This
Field cann't be empty."></asp:RequiredFieldValidator>
<asp:Button ID="Button1"
runat="server"
Text="Submit"
/>
Run this code . When user click on submit button without
filling the field then it will show the error message like ” This field cnn’t be empty.” (You can give any type
of error message according to you field.)
2.) RangeValidator Control:
This control is used
to test if the data entered by
the user in a data-entry-control is inside a specified range of values.
Add a RangeValidation Control on your web page by simple
drop and drag or doule click on the data-control .
When you add RangeValidationControl on your page , it add the following code on
your web page:-
<asp:RangeValidator ID="RangeValidator1"
runat="server"
ErrorMessage="RangeValidator"></asp:RangeValidator>
Example:
Validation.aspx:
<asp:TextBox ID="TextBox2"
runat="server"
style="z-index"></asp:TextBox>
<asp:RangeValidator ID="RangeValidator1"
runat="server"
ControlToValidate="TextBox2"
MinimumValue="18"
MaximumValue="60"
ErrorMessage="Age
should be in the range of 18 to 60."></asp:RangeValidator>
<asp:Button ID="Button1"
runat="server"
Text="Submit"
/>
Run this code . When user click on submit button without
filling the field then it will show the error message like ” Age should be in the range of 18 to 60.”
3.)RegularExpressionValidator :
This control is used
to test if the data entered by user in a data-entry control matches a
pattern defined by regular expression. A regular expression is a string that
match a set of string according to
certain syntax rules.
For example RegularExpression for email is : \w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
When you add RegularExpressionValidator on your page , it
add the following code on your web page:-
<asp:RegularExpressionValidator
ID="RegularExpressionValidator1"
runat="server"
ErrorMessage="RegularExpressionValidator"></asp:RegularExpressionValidator>
For example
Validation.aspx:
<asp:TextBox ID="TextBox3"
runat="server"
style="z-index"></asp:TextBox>
<asp:RegularExpressionValidator
ID="RegularExpressionValidator1"
runat="server"
ErrorMessage="please
enter your mail like sushilct26@gmail.com"
ControlToValidate="TextBox3"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
<asp:Button ID="Button1" runat="server" Text="Submit" />
Run this code . When user click on submit button without
filling the field then it will show the error message like ” please enter your mail like
sushilct26@gmail.com.”
4.)CompareValidationControl:
This control is used for compare the two data-entry control
field .i.e for password and compare
password when user sign up first time .
Add a CompareValidationControl Control on your web page by
simple drop and drag or doule click on the data-control .
When you add CompareValidationControl on your page , it add
the following code on your web page:-
<asp:CompareValidator ID="CompareValidator1"
runat="server"
ErrorMessage="CompareValidator"></asp:CompareValidator>
Example:
Validation.aspx:
<asp:TextBox ID="TextBox4"
runat="server"
style="z-index"></asp:TextBox>
<asp:TextBox ID="TextBox5"
runat="server"
style="z-index"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1"
runat="server"
ControlToValidate="TextBox4"
ControlToCompare="TextBox5"
ErrorMessage="Type
the same password as entered above."></asp:CompareValidator>
<asp:Button ID="Button1" runat="server" Text="Submit" />
Run this code . When user click on submit button without
filling the field then it will show the error message like Type the same password as entered
above.”
5.)CustomValidatorControl:
This validation control is
used to test the data-entry
control data in your own ways. But this
validation control has some restriction. You can use this control only for
range checking and comparisons. For
example if you want to check whether a number is even or odd then it is not possible
for existing validation control. For this type of problem we use
CustomeValidation control.
Add a CompareValidationControl Control on your web page by
simple drop and drag or doule click on the data-control .
When you add CompareValidationControl on your page , it add
the following code on your web page:-
<asp:CustomValidator ID="CustomValidator1"
runat="server"
ErrorMessage="CustomValidator"></asp:CustomValidator>
Example:
Validation.aspx:
<asp:TextBox ID="TextBox4"
runat="server"
style="z-index"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator1"
runat="server"
ControlToValidate="TextBox6"
ClientValidationFunction="validation"
ErrorMessage="Enter
the correct password."></asp:CustomValidator>
<asp:Button ID="Button1" runat="server" Text="Submit" />
Run this code . When user click on submit button without
filling the field then it will show the error message like Enter the correct password.”(This
message will pop up when you enter any thing instead of New York.)
6.) ValidationSummaryControl:
This control is used to display summary of all error message from all
validation control on a web page.
Add a ValidationSummaryControl Control on your web page by
simple drop and drag or doule click on the data-control .
When you add ValidationSummaryControl on your page , it add
the following code on your web page:-
<asp:ValidationSummary ID="ValidationSummary1"
runat="server"
/>
Example:
Validation.aspx:
<asp:TextBox ID="TextBox1"
runat="server"
style="z-index"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" style="z-index"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server" style="z-index"></asp:TextBox>
<asp:TextBox ID="TextBox4" runat="server" style="z-index"></asp:TextBox>
<asp:TextBox ID="TextBox5" runat="server" style="z-index"></asp:TextBox>
<asp:TextBox ID="TextBox6" runat="server" style="z-index"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1"
ErrorMessage="This
Field cann't be empty."></asp:RequiredFieldValidator>
<asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="TextBox2" MinimumValue="18" MaximumValue="60" ErrorMessage="Age should be in the range of 18 to
60."></asp:RangeValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ErrorMessage="please
enter your mail like sushilct26@gmail.com"
ControlToValidate="TextBox3"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToValidate="TextBox4"
ControlToCompare="TextBox5"
ErrorMessage="Type
the same password as entered above."></asp:CompareValidator>
<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="TextBox6"
ClientValidationFunction="validation"
ErrorMessage="Enter
the correct password."></asp:CustomValidator>
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
/>
<asp:Button ID="Button1" runat="server" Text="Submit" />
Run this code . When user click on submit button without
filling the field then it will show the error message from all validation.
Donn't forget to comment this post.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.