Introduction:

In this article, we discuss understanding the basic Asp.Net validation techniques and controls. We will try to understand which validation control should be used in any particular scenario and then make a small web application to illustrate the use of validation controls.

FREE PDF DOWNLOAD FOR MICROSOFT DOT NET COURSE MATERIAL

Why we use Validation Controls?

Validation is an important part of any web application. Users request must always be validated before sending across different layers of the application.

Types of Validation :

There are two types of Validation Techniques

  • Client Side Validation
  • Server-Side Validation

Client Side Validation :

Client-side validation is considered convenient for users as they get feedback. The important concept is that it prevents a page from being postback to the server until the client validation is executed successfully. it saves valuable resources of the server.

Server Side Validation :

Server-side validation is used by writing our custom logic for validating all the input. Asp.Net also provides some controls which will facilitate the server side validation and provides a framework for the developers to do the same.

Validation Controls in Asp.Net

The Most important aspect of creating Asp.Net Web pages for client request is to check whether the client input is valid.

Validation Controls are,

  • RequiredFieldValidator
  • RangeValidator
  • CompareValidator
  • RegularExpressionValidator
  • CustomValidator
  • ValidationSummary

RequiredFieldValidators :

The RequiredFieldValidator is actually very simple, and yet very useful. The RequiredFieldValidator control ensures that the required field is not empty.

The Syntax for RequiredFieldValidators

<asp:RequiredFieldValidator ID=”rfvuser”

runat=”server” ControlToValidate =”ddluser”

ErrorMessage=”choose a user”

InitialValue=” choose a user”>

</asp:RequiredFieldValidator>

Range Validators:

It control verifies that the input value falls within a  range.

The Syntax for Range Validators

<asp:RangeValidator ID=”rvcls” runat=”server” ControlToValidate=”txtcls”

ErrorMessage=”Enter your cls (7 – 15)” MaximumValue=”15″

MinimumValue=”7″ Type=”Integer”>

</asp:RangeValidator>

Compare validator:

The CompareValidator control compares a value in one control with a specified value or a value in another some control.

The Syntax for Compare Validator

<asp:CompareValidator ID=”CompareValidator2″ runat=”server”

ErrorMessage=”CompareValidator”>

</asp:CompareValidator>

RegularExpressionValidators:

It allows validating the input text by matching against a pattern of a regular expression. The regular expression is set in the ValidationExpression property.

The Syntax for RegularExpressionValidators

<asp:RegularExpressionValidator ID=”string” runat=”server” ErrorMessage=”Enter the string”

ValidationExpression=”string” ValidationGroup=”string”>

</asp:RegularExpressionValidator>

Custom Validator:

It allows writing application specified custom validation routines for both the server side and the client side validation.

The Syntax for Custom Validators

<asp:CustomValidator ID=”CustomValidator3″ runat=”server”

ClientValidationFunction=.cvf_func. ErrorMessage=”CustomValidator”>

</asp:CustomValidator>

Validation Summary:

Validation Summary control does not perform any validation but shows a summary of all errors in the page.

The Syntax for Validation Summary

<asp:ValidationSummary ID=”ValidationSummary2″ runat=”server”

DisplayMode = “BulletList” ShowSummary = “true” HeaderText=”Errors:” />

ASP.NET Useful Resources: Ten ASP.NET Article – You Should Not Miss