Working with validation controls in asp.net


In this article we will see how can we apply validations on control. There are various ways for applying validations on a control using javascript, from code behind. But here we will see it through the inbuilt validation controls available in the visualstudio tool.

Their are six types of validations:

1. RequiredFieldValidator
2. RangeValidator
3. CompareValidator
4. RegularExpressionValidator
5. CustomValidator
6. ValidationSummary

1. RequiredFieldValidator :With it we can check whether the user had entered a value in the
specific control or not.


<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtname" EnableClientScript="False"
ErrorMessage="Name Field is Required">*</asp:RequiredFieldValidator>


2. RangeValidator : With this we can set the specific minimum and maximum value for the
specific control.
Below demo shows how can we set the minimum and maximum value and the datatype for a textbox.


<asp:TextBox ID="txtage" runat="server"></asp:TextBox>
<asp:RangeValidator ID="RangeValidator1" runat="server"
ControlToValidate="txtage" ErrorMessage="Age Should be in required Limit"
MaximumValue="59" MinimumValue="18" Type="Integer">*</asp:RangeValidator>


3. CompareValidator : With this we can check the values entered by user with the another value.
even we can check for the data type also.
Below demo shows how can we check values for password and confirm password textbox is same or not
using CompareValidator.

<asp:TextBox ID="txtpassword" runat="server" TextMode="Password"></asp:TextBox>
<asp:TextBox ID="txtconfirmpassword" runat="server" TextMode="Password"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToCompare="txtpassword" ControlToValidate="txtconfirmpassword"
EnableClientScript="False" ErrorMessage="CompareValidator">*</asp:CompareValidator>


4. RagularExpressionValidator : Compares the value of control against the specified regular expression.
Below code contains how we can specify the regular expression for a control.

<asp:TextBox ID="txtemailid" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="txtemailid" ErrorMessage="Email Address Field is Required">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="txtemailid" EnableClientScript="False"
ErrorMessage="Email Is In Incorrect Format"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>



5. CustomValidator : In this we can set our own validation requirement. We can write custom validation.
Below demo shows how can we create the code for accepting only even numbers.

protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
if (int.Parse(txtnum.Text) % 2 == 0)
{
args.IsValid = false;

}
else
{
args.IsValid = true;
}
}

How we set the textbox to accept only even numbers.

<asp:TextBox ID="txtnum" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtnum" EnableClientScript="False"
ErrorMessage="Sr. Number Field is Required">*</asp:RequiredFieldValidator>
<asp:CustomValidator ID="CustomValidator1" runat="server"
ControlToValidate="txtnum" ErrorMessage="even should be there"
onservervalidate="CustomValidator1_ServerValidate">*</asp:CustomValidator>


6. ValidationSummary : It help to show the all the validation error messages on the page.

<asp:ValidationSummary ID="ValidationSummary1" runat="server" />

ValidationSummary has a ShowMessageBox property which we cans et if we want to see the error
messages in a popup box.

Note: 1. We have the Property ErrorMessage for all the validation controls which controls the text
that we want to show in the validationsummary when error occurs.

2. If we want to disable all validations we can set the CausesValidation property of button to false.
3. All the validation controls has ControlToValidate property which is mandatory to enter as it
contains the Id of the control on which we have to perform the validation.


Comments

Author: ketan Italiya04 Aug 2013 Member Level: Gold   Points : 0

thanks .

this article is so useful



  • 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: