ASP.NET validation controls provide an easy-to-use but powerful mechanism of ensuring that data is entered correctly on the forms. There are 6 validation controls included in the ASP.NET 2.0 and ASP.NET 3.5 versions.
ASP.NET validation controls also provide two ways of validation: Server-side or Client-side. The nice thing about these Validation controls is that it will perform client-side validation when it detects the browser is able (unless client-side validation has been disabled). Thus reducing roundtrips. And it will perform server-side where necessary. This client-side/server-side detection and validation is done without extra work by the developer.
In ASP.NET, there are five(6) Validation controls included. They are:
|Validation Server Control||Description|
|CompareValidator||Compares the value of one input control to the value of another input control or to a fixed value.|
|CustomValidator||Allows you to write a method to handle the validation of the value entered.|
|RangeValidator||Checks that the user enters a value that falls between two values.|
|RegularExpressionValidator||Ensures that the value of an input control matches a specified pattern.|
|RequiredFieldValidator||Makes an input control a required field.|
|ValidationSummary||Displays a report of all validation errors occurred in a Web page.|
How to use Validation Control in website
Create a new website project and you have an Empty default.aspx page. In your Solution Explorer double click it and go to its design view. Look for your toolbar on the left hand side of your window. In the Toolbar look for the Validation Section as depicted below:
These controls are used to validate the user input provided at the standard controls.
- If the validation control returns ‘false’ then the data will not be submitted to the server.
- A single validation control can validate a single standard control.
- A single standard control can be binded with any no. of validation controls.
- Validation scripts should not be altered from ASP. Net 2.0 onwards. If any modifications are performed on the scripts then all the ASP.Net websites which uses validation controls will not function properly.
- Validation controls can be grouped together from ASP. Net 2.0 onwards.
Difference Between Server side Control And Client Side Control
- Client side validation is processed the client side before submitting the form. The advantage of using the client side validation is it reduces the network traffic since the validation is processed in the client machine itself.
- Server side validation is processed in the server. Some data cannot be validated in the client side and it has to be validated in the server side. E.g. Date between the two dates in the database.
- Client-side is faster than server-side as the networking time from client to server is saved.
- server-side is done on the server. Then the server converts the data into an html page and sends to the browser.
- server-side is more secure as the user cannot see the code even he does a view-source.