Introduction
Validation plays an essential role in web applications. The user’s input must be validated before sending it across different application layers. The validation controls implement presentation logic to validate user input, data format, data type, and date range.
The validation is further divided into two categories:
-
Client-side validation: This validation is convenient for users as they get instant feedback. The main advantage of this validation is that it prevents the page from being postback to the server until the client-side validation is executed successfully. It is dependent on the browser and scripting language.
- Server-side validation: This validation is preferable from a developer’s point of view as it will not fail. It is not dependent on the browser or scripting language.
ASP.NET validation ensures both client-side and server-side validation. First, it works on the client-side validation and then on the server-side validation. The server validation will always run whether client validation is executed or not. Hence, we have safety of validation check.
Validation Controls in ASP.NET
A crucial aspect of creating ASP.NET web pages for user input is to check that the user’s entered input is valid. The ASP.NET provides a set of validation controls that provide an easy yet powerful way to check for errors and displays messages to the user.
Following are the six types of validation controls in ASP.NET along with their working:
-
RequiredFieldValidation Control: It makes an input control a required field.
-
CompareValidator Control: It compares the value of one input to another. It can also be used to fix a value.
-
RangeValidator Control: It is used to check whether the input entered by the user falls under the required criteria.
-
RegularExpressionValidator Control: It ensures that the value of an input control matches the specified pattern.
-
CustomValidator Control: This allows writing a method to validate the entered value.
- ValidationSummary: It displays a report of all the validation errors that occur on a web page.
This blog will discuss one of the validation controls, i.e., compare validator with a few examples.