Validation Controls In ASP.NET

Introduction 

 
In this article, we are going to learn validation controls in ASP.NET which we can use for validation purposes. In ASP.NET, there are six different types of validation control present that we can use. If the user does not enter the proper input, then it will throw an error using these controls.
 
Read my previous blog on validations in MVC so you can learn more on validation before reading this article.
Below is the validation control list which we can use in ASP.NET:
  1. RequiredFieldValidator
  2. RegularExpressionValidator
  3. RangeValidator
  4. CompareValidator
  5. CustomValidator
  6. ValidationSummary 
There are a couple of properties that are the same while using these validation controls, such as ControlToValidate, and ErrorMessage.These validation controls inherit properties and methods of the BaseValidator class. Validation control provides validation on both client-side and server-side. Let's understand each control in detail below.
 

RequiredFieldValidator

 
It is used to make sure that users enter input or not. It is nothing but whatever control you are using it makes the field mandatory. You must need to enter a value, otherwise it will throw an error message. You can set two properties for this as we mentioned below ControlToValidate and ErrorMessage. ControlToValidate is nothing but the ID of your control which is associated with the control that you are using like TextBox.ErrorMessage is used to display error messages on the screen. You can set color as well for displaying an error message.
 

RegularExpressionValidator

 
It is mostly used for the expression pattern that we want, for example, with Email we need @ keyword to see what is the size of the email, etc. It performs validation according to a regular expression. For example, as I said above Email, Phone, Date, area code, pin code etc. These controls use the ValidationExpression property to set valid regular expression.
 

RangeValidator

 
Range Validator is used for values that the user enters to see if it falls between two values or not. You need to set MinimumValue and MaximumValue. You can enter values like Currency, Date, Integer, Double, and String.
 

CustomValidator

 
The developer can write a method to handle the validation of the value entered by the user. It is used to create your own validation. It performs the validation code that you wrote. 
 

CompareValidator

 
It compares the values of one input control to value of the another input control. You can compare value to a fixed one as well. There are a couple of important properties that we can use while using CompareValidator. These include ValueToCompare, ControlToCompare, and Type.
 

ValidationSummary

 
This will display all your error message information on your web page. There are a couple of properties by using this we can display all errors in a different format. For example, in DisplayMode we can display messages in a bulleted list or in a single paragraph. By using HeaderText property, we can display header text above the validation summary.
 
I hope you guys understand the validation controls in ASP.NET. Let me know if you guys have any questions!