Updated March 14, 2023
Overview of ASP.Net Validation Controls
ASP.Net provides various validation controls that validate the user data to ensure that the data entered by the user are satisfied with the condition. ASP.Net provides RequiredFieldValidator, RangeValidator, CompareValidator, RegularExpressionValidator, CustomValidator and ValidationSummary. RequirefFieldValidator is used when the specific filed is required, RangeValidator is used when specific value needs to be within the specified range, CompareValidator is used to compare the value of one control with fixed value of another control, RegularExpressionValidator validate the input value with the pattern of regular expression, CustomValidator allows user to create own validation, ValidationSummary gives summary.
How do they work?
To understand the working of ASP.Net Validation Controls, let us take a look at the class which all controls inherit. If you are not familiar with the concept of inheritance, we recommend you understand it before proceeding.
The BaseValidator Class
All the validation controls in ASP.Net inherit the properties and methods of the BaseValidator class. It helps in making a generic suite of validation controls. The important properties and methods of the BaseValidator class are:
- ControlToValidate – It indicates the input control to validate. It must be a unique value throughout the form. This attribute is a mandatory one as it is used to associate the input control with a validation control.
- Enabled – It enables or disables the validator.
- Text – It holds the message to be displayed in the event of a validation failure.
- ErrorMessage – The value in this attribute is displayed either when ValidationSummary control is used or when Text property is missing.
- IsValid – A Boolean attribute which indicates whether the control is valid or not.
- Validate() – This method revalidates the control and updates the IsValid
Types of ASP.Net Validation Control
Following are the types of ASP.NET validation controls:
This is an elementary validation control. Almost all the forms have some fields that are mandatory to be filled up by the user before proceeding forward. The Required Field Validator ensures that such fields are not left empty.
<asp:RequiredFieldValidator ID="someUniqueId" runat="server" ControlToValidate ="someUniqueControlId" ErrorMessage="ErrorToDisplayOnValidationFailure" InitialValue="aPlaceholderValue"> </asp:RequiredFieldValidator>
Notice the Initial Value attribute in the syntax above. This although acts as a placeholder, in addition, the validation fails if the value of the field does not change from the InitialValue on losing focus.
The RangeValidator control simply specifies the permitted range within which the input value should fall. This is most helpful for numeral input values such as age or for Date input values.
<asp:RangeValidator ID="some unique id" runat="server" ControlToValidate ="someUniqueControlId" ErrorMessage="ErrorToDisplayOnValidationFailure" Type="Integer" MinimumValue=”0” MaximumValue=”100”> </asp:RangeValidator>
There are three additional properties of this control
- Type – Specifies the type of data in the input field. The value is converted to this type before validation. An exception is thrown if the value cannot be converted to the specified type. The following data types can be compared in RangeValidator:
- MinimumValue – Specifies the minimum value that the field can hold.
- MaximumValue – Specifies the maximum value that the field can hold. Both minimum and maximum values are inclusive invalidations.
- RegularExpressions, or simply Regex, are patterns that define the format of the text. If the text is in the same format, Regex returns true, else false. It is recommended to read about Regex if you are not familiar with it. This will also give you an idea about how Regex patterns are formed and how to decipher a Regex pattern.
- Thus, a RegularExpressionValidator is a very versatile validation control. It matches the input text against the pattern specified in the ValidationExpression property.
<asp:RegularExpressionValidator ID="someUniqueId" runat="server" ControlToValidate ="someUniqueControlId" ErrorMessage="ErrorToDisplayOnValidationFailure" ValidationExpression=”aRegexPattern”> </asp:RegularExpressionValidator>
An example of a Regex pattern can be ^[a-z][0-9]. This indicates that a text must start with an alphabet and follow by a numeral.
- The CompareValidator control compares the value of one control with either a fixed value or a value in another control.
<asp:CompareValidator ID="someUniqueId" runat="server" ControlToValidate ="someUniqueControlId" ErrorMessage="ErrorToDisplayOnValidationFailure" Type="string" ControlToCompare=”ControlToValidateIdOfAnotherControl” ValueToCompare=”aFixedValue” Operator=”Equal”> </asp:CompareValidator>
There are three new properties in this validator:
- ControlToCompare – It holds the ControlToValidate Id of another form of control. The value of both the form fields is then compared.
- ValueToCompare – A fixed value with which the comparison has to be made.
- Operator – The type of comparison. The allowed values in this attribute are:
- ASP.Net also allows the freedom of writing your own validator. This eases the task of a developer to validate the form at the client side itself. It also allows putting more complex validations in place. Validations that are business or application-specific can be written using custom validators.
- The custom validation code is written in a function in the code-behind page and the function name is passed as an attribute to the CustomValidator class. Custom validation can be done either at the client-side or the server-side.
- The ServerValidate event handler is used when validation has to be done on the server-side. The server-side validation routine is written in C#, VB .Net or any other .Net language.
<asp:CustomValidator ID="someUniqueId" runat="server" ControlToValidate ="someUniqueControlId" ErrorMessage="ErrorToDisplayOnValidationFailure" ClientValidationFunction=”functionName”> </asp:CustomValidator>
The ValidationSummary control does not perform any validation. Its purpose is to display a summary of all the errors on the page.
<asp:ValidationSummary ID="ValidationSummaryControl" runat="server" DisplayMode=”BulletList” ShowSummary=”true” HeaderText=”List of Errors” />
This article covers the basics of validation controls in ASP .Net. There is a lot more to discover once you start building your web form. For advanced coders, we recommend MSDN documentation for a thorough understanding of the topic.
This has been a guide to ASP.Net Validation Controls. Here we discussed the basic concept, types with the syntax of ASP.Net Validation Control respectively. You can also go through our other suggested articles to learn more –