ASP.NET Validation Controls


If user enter some invalid information as a input you must protect that for that reason ASP.net introduce a concept called as Validaion Controls. Using this we can restrict unwanted infromation entered by user. There are 6 types of validation controls avaialble out of 6 validations 5 of them are used to perform actual validation, whereas the final control - validationsummary is used to provide feedback to the user any errors in the page.

ASP.NET Validation Controls:


ASP.net Introduces 6 type of validation controls to perform validations in your application. Out of 6, 5 of them are used to perform actual validation, whereas final validation control name as "Validation Summary" control is used to summarize to user about the error details.

They are 6 types of validation controls available in ASP.net:


• Compare Validator
• Custom Validator
• Range Validator
• RegularExpression Validator
• RequiredField Validator
• Validation Summary.



1

Now, I’m trying to explain each and every validation control how it’s works in our Application.
Below are the common controls for all validation controls.
CssClass – This property helps you to apply the style for Error message.
Text – This property helps you to display the text for validation summary.
ControlToValidateM – This property helps you to target the id of the control to validate.
EnableClientScript – It refers the Client Side validations is enabled or not.
SetFocusonError – This property helps you whether the client side script focus the control or not.
validationGroup – This property helps you to group some of controls together to perform the action. If anyone of the control is not performing the action then this property will raise an issue. For ex Login form, in login form we must enter username as well as Password, if we are not enter any one of these then obviously its raises an error message.

RequiredFieldValidator:


The Required Field validator control, to make sure that the user must enter some text in that target control otherwise it’s raise an error message.

<strong>RequiredField Validator</strong><br />
<br />
<br />
Enter User Name :
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>


<asp:RequiredFieldValidator ID="req1" runat="server" ControlToValidate="txtName" ErrorMessage="*"></asp:RequiredFieldValidator>


<br />
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
<br />


2

RangeValidator:


The RangeValidator enables you to check whether a value falls within certain range. The controls check data types like string, numbers, dates and currencies.

Properties:
MinimumValue – determine the lowest acceptable value.
MaxValue – determine the highest acceptable value.
Type – This property determine the data type that the validation control checks. Several types are listed out String, Integer, Double, Date, Currency.

<strong>RangeField Validator<br />
<br />
</strong>Enter Age :
<asp:TextBox ID="txtAge" runat="server"></asp:TextBox>


<asp:RangeValidator ID="rv" runat="server" ControlToValidate="txtAge" MinimumValue="1" MaximumValue="99" Type="Integer" ErrorMessage="*"></asp:RangeValidator>


<br />
<br />
<asp:Button ID="btnSubmit1" runat="server" Text="Submit" />
<br />


3

Compare Validator:


The compare Validator control is used to compare one value control to another value control. Mostly this type of validation we are using to compare Password and Confirm password scenarios.
Properties:
ControlToCompare – this Property contains the ID that the validator compares against the control.
Operator – This property determine to compare the operator. Types of operators available Equal, Not equal, lessthanEqual, GreaterThanEqual..


<br />
Value :
<asp:TextBox ID="txtVal" runat="server"></asp:TextBox>
<br />
Compare Value :
<asp:TextBox ID="txtVal1" runat="server"></asp:TextBox>


<asp:CompareValidator ID="cv" runat="server" ErrorMessage="*" ControlToCompare="txtVal1" ControlToValidate="txtVal" Type="Integer" Operator="Equal"></asp:CompareValidator>


<br />
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />


4

RegularExpression Validator:


Regular Expression validator control enables you to check a value against a regular expression. Using validationExpression property we can able to check the Regular expression. In below scenarios we can use the Regular Expressions ex: mail id, phone number, zip code, address etc..

</strong><span class="style2">Email : </span>
<asp:TextBox ID="txtmail" runat="server"></asp:TextBox>


<asp:RegularExpressionValidator ID="rev" runat="server"
ErrorMessage="*" ControlToValidate="txtmail" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ></asp:RegularExpressionValidator>


<br />
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />


5

Custom Validator:


When none of other validation controls won’t help to validate then we are using Custom validator control to validate the control. Using this we can able to prepare our own custom validation method and call that method using OnServerValidate property. This type of validation we can able to perform while enter password minimum characters, in that case we can able to perform the validation using custom validation property.

Custom Validator Control<br />
<br />
</strong><span class="style2">Password : </span>
<asp:TextBox ID="txtPwd" runat="server"></asp:TextBox>


<asp:CustomValidator ID="cv" runat="server" ControlToValidate="txtPwd" OnServerValidate="Custom_Validate" ErrorMessage="*"></asp:CustomValidator>


<br />
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />


6

Validation Summary:


Validation summary control is used to validate all the controls. For ex in a page we have no.of controls to validate in that case we use validation summary control to validate all the controls and display error message in our page.
Ex:

</strong><span class="style2">Enter User Name</span><strong> :
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="req1" runat="server" ControlToValidate="txtName" ErrorMessage="Enter User Name" Text="*"></asp:RequiredFieldValidator>
<br />
<br />
</strong><span class="style2">Enter Age</span><strong> :
<asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtAge" ErrorMessage="Enter Age between 1 to 99." Text="*"></asp:RequiredFieldValidator>

<asp:RangeValidator ID="rv" runat="server" ControlToValidate="txtAge" MinimumValue="1" MaximumValue="99" Type="Integer" Text="*" ErrorMessage="Enter Age between 1 to 99."></asp:RangeValidator>
<br />
</strong><br />
</strong><span class="style2">Email : </span>
<asp:TextBox ID="txtmail" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtmail" ErrorMessage="Enter proper email" Text="*"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="rev" runat="server" Text="*"
ErrorMessage="Enter proper email" ControlToValidate="txtmail" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ></asp:RegularExpressionValidator>
<br />
<br />


<asp:ValidationSummary ID="vs" runat="server" HeaderText="You must enter the following fields" DisplayMode="BulletList" EnableClientScript="true" />


<asp:Button ID="btnSubmit" runat="server" Text="Submit" />


7

Difference between the Text and ErrorMessage properties :


Both of them perform the same action but when we declare both in a control at the same time, the validation control displays the text property result instead of Error Message.

Conclusion :


This article will help you those who are looking for the validations to perform in an Application page.


Comments

Author: Ritesh24 Feb 2014 Member Level: Bronze   Points : 0

Very Nice Post, I never see such type of post ever.



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