Programmingempire
Basically, validation is one of the most significant tasks in creating a web application. This article will provide several examples of Validation Controls in ASP.NET.
Validation Controls in ASP.NET
In fact, ASP.NET provides following six types of validation controls that you can use to validate the data of a Web Form.
- RequiredFieldValidator
- CompareValidator
- RangeValidator
- RegularExpressionValidator
- CustomValidator
- ValidationSummary
Required Field Validator
Whenever you want to make an input field mandatory, you can use a required field validator control. The name of the class in the .NET Framework for this control is RequiredFieldValidator and it makes the associated field a required field. Accordingly, whenever, the field associated with this validator is left empty by the user, the validation fails and the form is not submitted. Usually, this validator control is used with other validators such as the Range validator or the Regular Expression Validator.
Compare Validator
In case you want to compare the content of two fields you can make use of the Compare Validator. The class available in .NET Framework for this purpose is named CompareValidator. As can be seen in the example, you can compare for equality. However, the control provides you other options too. Hence you can use any of these operators for comparison – Equal, NotEqual, LessThan, GreaterThan, LessThanEqual, GreaterThanEqual, and DataTypeCheck.
Additionally, this control also has a ValueToCompare property that you use to specify a constant value. In case, you don’t want to compare two fields of the form and rather want to match the value of a field with a constant value, the above-mentioned property is useful.
Since, comparison also requires the data type check, the Compare Validator control also provides a Type property that takes any of the following values – Integer, String, Double, Date, and Currency.
Range Validator
In a similar manner, you can use a Range Validator control to determine whether the value entered by the user falls within a given range or not. The .NET Framework provides a class called as RangeValidator for this purpose.
Since checking the range of a value also depends on the data type, this control also has a Type property similar to the Compare Validator control. Additionally, there are two properties called as MinimumValue and MaximumValue, that you can use to specify the range.
Regular Expression Validator
In order to to use a regular expression for validating the value of a field, you can use a Regular Expression Validator and the name of the corresponding class is RegularExpressionValidator.
Whenever you want that the input value in a field follows a certain pattern of characters, you can use this validation control. Consequently, this validation control has applications in determining the validity of the formats of the URLs of a website, email addresses, phone numbers, zip codes, and so on. Further, it has a ValidationExpression property that allows you to specify the regular expression that you want to match.
Custom Validator
In many situations, any of the above-mentioned validation controls are not sufficient for validating the value of a field, you can use a Custom Validator control. Accordingly, the corresponding .NET class is called as CustomValidator. This validator allows you to create your own custom validation method and use it with the particular custom validation control.
Hence, you can create a user-defined method for input validation and associate it with the event handler function of the ServerValidate event.
Validation Summary
As the name suggests, the Validation Summary control lets the user display the summary of validations performed on the current page. Therefore, the ValidationSummary control displays all validation errors on the same page. This is particularly useful in web forms containing many fields where tracking the errors in specific input fields becomes cumbersome.
Summary
This article has explained the server-side validation using several Examples of Validation Controls in ASP.NET. Basically, ASP.NET provides six validation controls that we can use to perform validation of input values entered by the user in a Web Form control.
Further Reading
Parameter and ParameterCollection in ADO.NET
Database Manipulation Using DataGrid
Example of Button and Link Button Control in ASP.NET
Example of Chart Control in ASP.NET
Creating a DataTable from a DataReader in ASP.NET
Deleting a Record using DataGrid Control in ASP.NET
Edit a Record Using DataGrid Control in ASP.NET
Insert a Record Using ItemCommand Event in DataGrid
CRUD Operations with DataGrid in ASP.NET
Creating Columns in a DataGrid Control
XML Documents and DataSet in ASP.NET
ASP.NET Core Features and Advantages
Display Images Using DataList Control
Adding Images Using Image Control
Creating a Group of Radio Buttons Using RadioButtonList Control
Example of Button Control in ASP.NET
ItemDataBound Event in DataList
More Features of DataList in ASP.NET
A Simple Example of Using a DataList Control in ASP.NET
Properties and Methods of DataList Control in ASP.NET
Exploring DataList Control in ASP.NET
Custom Validator Control in ASP.NET
Validation Summary Control in ASP.NET
Validation Controls Examples – RequiredFieldValidator, CompareValidator, and RangeValidator
An Example of Data Binding with RadioButtonList Control
Binding Data to Web Control in ADO.NET
Examples of AdRotator Control in ASP.NET
Examples of Validation Controls in ASP.NET
Overview of MVC architecture in ASP.NET
- Angular
- ASP.NET
- C
- C#
- C++
- CSS
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- PHP
- Power Bi
- Python
- Scratch 3.0
- TypeScript
- VB.NET
