ASP.NET: Introduction to Validation Techniques or validations Controls in asp.net.

Aman Sharma
0
Validation in Asp.NET

In this article I will explain different type of validations in asp.net. We will learn why we need validations in website development. The main motive of using validations is to validate input data from users. We validate controls so that they accept only correct data or data in valid format.

Why we need Validations:

Validation is required when we want input in particular format. Suppose we want to enter numeric no in TextBox control. So in that case we require validation. Suppose we want to enter email address in textbox then in that we will have to validate that particular textbox. We can validate controls for data type, empty control, special format, range etc.

Types of validations in Asp.NET:
   1.      Client side validator
   2.      Server Side validator

Server side validations: Server side validations are more secure. We use code in code behind to validate the data. So it is bit slow when compared to client side.

Client side validations: Client side validations are fast and easy to implement. Asp.Net Provides some validation controls to implement client side validations.

Validations controls in Asp.NET

  •         RequiredFieldValidator
  •         RangeValidator
  •         CompareValidator
  •         RegularExpressionValidator
  •         CustomValidator
  •         ValidationSummary
Main three properties are common in most of the controls:

ControlToValidate: Id of the control which we want to validate
Error Message: Error message which will be shown when error occurs
Text: Text which will be shown.

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 message in  one place.

Main prioperties are:

ShowMessageBox: it will show error in message box.
ShowSummary:It will show messages as a list.


Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !