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.
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.
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.
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.
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.
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. Also, an example of ValidationSummary control can be found here.
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.