Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Monday, 13 July 2015

Introduction: 
In this article I will explain different type of validations in asp.net. Asp.Net provides some validation controls to validate data.
Validations controls in Asp.Net

A.      RequiredFieldValidator
B.      RangeValidator
C.      CompareValidator
D.     RegularExpressionValidator
E.      CustomValidator
F.      ValidationSummary

1.     Required Field Validator: Required field validator is used to when we want to check whether control is empty or not. It ensures that the control is empty or not. We generally validate textbox with this control. We can also validate dropdownlist control.
Syntax to validate TextBox using Requiredfieldvalidator:
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Enter Name" ControlToValidate="txtName">*</asp:RequiredFieldValidator>

2.            RangeValidator Control
The RangeValidator control verifies that the input value falls within a predetermined range.
Syntax for Range validator:
  <asp:RangeValidator ID="rvValue" runat="server" ControlToValidate="txtValue"
   ErrorMessage="Enter Value between (10 - 20)" MaximumValue="20"
   MinimumValue="10" Type="Integer">

3.     CompareValidator Control
The CompareValidator control compares a value in one control with a fixed value or a value in another control.
Syntax  CompareValidator Control:
       <asp:CompareValidator ID="CompareValidator1" runat="server"
            ErrorMessage="CompareValidator" ControlToCompare="txtConfirmPassword"
            ControlToValidate="txtName"></asp:CompareValidator>

4.     RegularExpressionValidator
The RegularExpressionValidator is used in case of validating the input text by matching against a pattern of a regular expression. The regular expression is set in the ValidationExpression property.
We can use other regular expression to validate data which are not listed in ValidationExpression property.
Syntax for regular expressions:
<asp:RegularExpressionValidator
                ID="RegularExpressionValidator1" runat="server"
            ErrorMessage="Enter Valid Email" ControlToValidate="txtEmail"
            ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>

5.     CustomValidator:
The CustomValidator control is used to write custom validation for both the client side and the server side validation.
For client side validation we use ClientValidationFunction property. For server side we use onservervalidate property. We call function created on server side.
Syntax For custom Validator:
<asp:CustomValidator runat="server" id="customname" controltovalidate="txtName" onservervalidate="function_name" errormessage="The text must be exactly 10 characters long!" />


6.     Validation Summary: Validation summary is not used to validate controls. It is used to show all error messages in one place.


0 comments:

Post a Comment