Introduction to Validation in ASP.NET
The following article provides an outline for Validation in ASP.NET. Validation is used to validate the user’s data. Whenever an application takes user input, it becomes essential to ensure the validity of the end-user’s data. Sometimes it is mandatory for the user to enter certain data. In some cases, the user data has to be in specification format. For example, phone number, email address, age limit, etc. There could be a situation when the data has to be in some range. For all of these situations, if we take the user inputs without validation, then there is a risk of storing the wrong data in the database. The application might also end up behaving in an expected manner or even crash the system. So it is always a worthy awareness to have validation in place whenever taking input from the user.
Types of Validation in ASP.NET
In ASP.NET, there are two types of validation:
- Client Side Validation
- Server Side Validation
1. Client Side Validation
Validation which is performed on user’s browsers is called client side validation. It will occur before the data gets posted to a server. It is a good option to have the client side validation as the user will get to know what needs to be modified immediately. So there will be no trips between client and server. So from a user point of view, it gives him a faster response, and from a developer’s point of view, it saves the server’s valuable resources. ASP.NET provides some validation controls to perform the validation on the client side. With the help of this validation control, developers can get out client side validation in place without writing a lot of code.
2. Server Side Validation
Validation which is performed on a server computer is called as server side validation. The advantage of server side validation is that if the user somehow avoids the client side validation, the programmer can catch the server’s problem. Therefore, server side validation provides additional security and certifies that no invalid data get Managed by the application. Validation at the server side is performed by writing our custom logic for validating all user input. ASP.NET provides some validation controls, facilitating the service side validation and offering a framework for a programmer to do the same. Web developer selects any type of validation. But generally, it is good to have client side validation and the same validation on the server side. Server side validation takes some web server resources to validate the already validated data, which is from the client side, but it ensures security.
ASP.NET Validation Controls
ASP.NET validation controls validate the data entered by the user. If the data does not pass validation, it will display an error message to the user.
- CompareValidator: This validator validates the value of one input with the value of another input. ControlToValidate, ControlToComapre, ValueToCompare and ErrorMessage are the properties of CompareValidator.
- RangeValidator: This validator determines whether the values entered by the users fall between two values. ControlToValidate, MaximumValue, MinimumValue and ErrorMessage are properties of RangeValidator.
- RequiredFieldValidator: This validator makes an input control a required field. ControlToValidate and ErrorMessage are properties of the RequiredFieldValidator.
- CustomValidator: This validator allows the user to write a method to handle the validation of the value entered. ControlToValidate, ClientValidationFunction, ErrorMessage and ServerValidate event are properties of the CustomValidator.
- RegularExpressionValidator: This validator ensures that the value of an input control matches a specified pattern. ControlToValidate, ValidationExpression and ErrorMessage are properties of the RegularExpressionValidator.
- ValidationSummary: This validator displays a report of all validation errors that occurred on a web page.
BaseValidator class provides core implementation for all validation controls.
Properties of this class are as follows:
- BackColor: It is the background color of the CompareValidator control.
- EnableClientScript: It is a Boolean value that specifies whether the client side validation is enabled or not.
- Enabled: It is a Boolean value that specifies whether the validation is enabled or not.
- ControlToValidate: It specify the id of the control to validate.
- ForeColor: It specify the foreground color of the control.
- IsValid: It is a Boolean value that indicates whether the control specified by the ControlToValidate is determined to be valid.
- ErrorMessage: It is a text that displays in the ValidationSummary control when validation fails.
- Display: It displays the behavior of the validation control. It can be None, static or dynamic. None is used to show the error message only in ValidationSummary control; static displays an error message if validation fails. Space is reserved on the page for the message even if the input passes validation. Dynamic displays error message if the validation fails. Space is not reserved on the page for the message if the input passes validation.
Here in these validation in ASP.NET articles, we have seen the validation and its two types of client side validation and server side validation. We have also seen the validation controls that are used in ASP.NET with its properties.
This is a guide to Validation in ASP.NET. Here we discuss the introduction, types, ASP.NET validation controls and BaseValidator class. You may also have a look at the following articles to learn more –