Types of Validation Controls in ASP.NET!

1. Introduction

ASP.NET is a popular, open-source app development framework that enables developers to create dynamic web pages and design robust websites. This framework comes with various validation controls that enable the developers to set properties that can validate the user data to ensure that the entered data satisfies the condition. Some of the popular ASP.NET validators are RequiredFieldValidator, CompareValidator, RangeValidator, RegularExpressionValidator, ValidationSummary, and CustomValidator. These validations make business websites look more professional and also enable valid users to get access. This is the only reason any business owner would prefer to hire a .NET developer from a top .NET software development company. To know more about the types of validation controls of ASP.NET and see how developers can use them for web development, let’s go through this blog. 

2. Why Do We Use Validation Controls?

Validation controls are generally used to validate the user’s input data as per the specified range before it is sent to different layers of the web application. And this is why they are known as the most essential part of web apps. The top reasons for using validation controls are-

  • To validate user input value.
  • Helps in implementing presentation logic.
  • It enables the use of data format, data range, and data type for validation.

3. How Do They Work?

When it comes to understanding the working of ASP.NET Validation Controls, having knowledge of the class that inherits all the controls is necessary. This class is called ‘The BaseValidator Class’. All the validation controls in .NET inherit the methods and properties of the BaseValidator class. This enables the developers to make a generic suite of validation controls.

BaseValidator class

  • ControlToValidate – This is a property that indicates the input control to validate. Basically, the input value throughout the form must be unique. And this is why it is a mandatory attribute as it helps in associating the input control with a data validation control.
  • ErrorMessage – This property holds the message that is to be displayed in the event when the validation fails. 
  • Text –  The value in this property is going to be displayed when ValidationSummary control is used or there is a missing Text property.
  • Enabled – It is a property that enables or disables the validator.
  • Validate() – It helps in revalidating the control and updating the IsValid.
  • IsValid – It is an attribute that specifies whether the input control is valid or not.

4. Validation Controls in ASP.NET

Validation Controls in ASP.NET

Some of the most popular .NET validation controls that developers use are – 

4.1 RequiredFieldValidator Control

RequiredFieldValidator is known as an elementary validation control. There is no form that doesn’t consist of fields that are mandatory to be filled. If the users want to proceed with the form, they will have to mandatorily fill these fields. To ensure that such fields are not left empty, RequiredFieldValidator is used. Basically, this validation checks that there must be some value-added within the control. Some important properties of RequiredFieldValidator are-

  • Initial value:- Initial value is displayed by default to guide the users on how the value must be added. This property is also used by the developers for a drop-down list.
  • ControlToValidate:- This control is used to set the field of the text box for validation.
  • Text:- The text value is set for the validation under this property. 

Syntax of RequiredFieldValidator

<asp:requiredfieldvalidator id="UniqueId" runat="server" controltovalidate="UniqueControlId" errormessage="ErrorMessageForFailure" initialvalue="aPlaceholderValue">
 
</asp:requiredfieldvalidator>

4.2 RangeValidator

The RangeValidator is a validation control that is used by the .NET developers to check whether the value of the input control is inside some specific range or not. This type of control is used when it comes to getting inputs like Age, Date of Birth, or mobile numbers from the user on the websites. Some of the major properties of RangeValidator control are- 

  • Minimum Value:- This property is used by the developers to hold the valid range’s minimum value.
  • Maximum value:- This property is used to hold a valid range’s maximum value.
  • ControlToValidate:- ControlToValidate enables the developers to set the specific control that needs to be validated.
  • Type:- Here, Type properties are set after the above properties if necessary. RangeValidator can compare the following data types:
  1. String
  2. Integer
  3. Double
  4. Date
  5. Currency

Syntax of RangeValidator

<asp:rangevalidator id="UniqueId" runat="server" controltovalidate="UniqueControlId" errormessage="ErrorMessageForFailure" type="Integer" minimumvalue="”10”" maximumvalue="”500”">
 
</asp:rangevalidator>

4.3 RegularExpressionValidator

RegularExpressionValidator or Regex is a control that holds various patterns which clearly define the format of the text. This means that if the text that has been added is in the same format then the Regex control will return true or else false. This type of validation control is generally used to validate input fields like email, phone, Zip code etc. Some of the most important elements used to make the regular expression in this control are-

  • \d:- [0->5] (for value 0 to 5, matches decimal characters)
  • \D:- Other than [0->9] (matches non-decimal characters)
  • {Length}:-{min ,max}
  • \s:- space
  • \S:- other than space
  • |:- OR
  • \w:- [a->z][A->Z][0->9]
  • ( ):- a Validation group 
  • [ ]:- choice of given character

Syntax of RegularExpressionValidator

<asp:regularexpressionvalidator id="someUniqueId" runat="server" controltovalidate="someUniqueControlId" errormessage="ErrorToDisplayOnValidationFailure" validationexpression="”aRegexPattern”">
 
</asp:regularexpressionvalidator>

4.4 CompareValidator Control

CompareValidator control compares the input control of one field to fixed value or another control. This checks whether the entered value is the same or not. So, if in any case, both entered values are not the same, it will give validation errors to the users. This type of validation control is generally used in change password functionality to confirm new password and date range fields like start date and end date pair.  Some of the most specific properties used under this control for different types of comparisons are – 

  • Equal:- This property of CompareValidator is used by developers to check if the user input data is equal.
  • Not Equal:- It is a property that is used to check if the controls are not equal.
  • LessThan:- It checks out for less than a relationship.
  • LessThanEqual:- It figures out for less than equal relationships.
  • Greater than:- This property is used to check for a greater than relationship.
  • GreaterThanEqual:- It is a property that is used by the developers to check for the GreaterThanEqual relationship.

Syntax of CompareValidator

<asp:comparevalidator id="UniqueId" runat="server" controltovalidate="UniqueControlId" errormessage="ErrorMessageForFailure" type="string" valuetocompare="”anyFixedValue”" operator="”Equal”">
 
</asp:comparevalidator>

4.5 CustomValidator Control

CustomValidator is a control that is used by the .NET software development companies to customize and implement data validation as per the requirements and conditions that occur. For instance, if the developer wants to check whether the entered number is even or odd, then the existing controls cannot be used and for this, the developers use CustomValidation controls. Some of the major properties of custom validator controls in .NET are-

  • ClientValidationFunction:- This property is used to set the name of the function in the custom client-side script that is used for validation. In simple words, this property is used to do client-side validation as well as server-side validation and must be written in a scripting language, such as JavaScript or VBScript. 
  • ValidateEmptyText:- It is a property that is used by the .NET developers to set a Boolean value that indicates whether the empty text is valid or not.

Syntax of CustimValidator

<asp:customvalidator id="UniqueId" runat="server" controltovalidate="UniqueControlId" errormessage="ErrorMessageForFailure" clientvalidationfunction="”functionName”">
 
</asp:customvalidator>

4.6 ValidationSummary Control

ValidationSummary is a control that is used to display error messages. It is a control that collects every type of validation control error message and is used by the other validation controls on a web page. It then displays the error message on the screen. Some of the major properties of validation summary control are – 

  • Forecolor:- This is a property that is used to set the foreground color.
  • DisplayMode:- It is a property that is used by the developers to set the display mode of the control.
  • HeaderText:- Header text is set at the top of the summary.

Syntax of ValidationSummary

<asp:ValidationSummary ID="ValidationSummaryControl"
 
runat="server" DisplayMode=”BulletList” ShowSummary=true”
 
HeaderText=”List of Errors” />

5. Validation Groups

In ASP.NET, complex pages come with various different groups of data that are present in different panels. And in this case, a requirement for performing validation might arise which is divided separately into separate groups. This type of situation is managed by the .NET developers with the use of validation groups. For creating a validation group, the developer must put the validation controls and input controls into the same logical group which can be done by setting their ValidationGroup property.

6. Conclusion

As seen in this blog, there are some most popularly used validation controls in ASP.NET. All these validation controls be it a client validation or a server validation, everything is used to provide the required functionality to a web development project. This is known as a great improvement for ASP.NET as these controls make the .NET developer’s life very easy. Basically, these validation controls and web controls together help the developers to create smart digital forms.

profile-image
Itesh Sharma

Itesh Sharma is core member of Sales Department at TatvaSoft. He has got more than 6 years of experience in handling the task related to Customer Management and Project Management. Apart from his profession he also has keen interest in sharing the insight on different methodologies of software development.

Comments

  • Leave a message...

    1. Rahul Mahadik

      I am a .NET developer and I know very well why validation is so important for any project. This article provides a detailed overview of validation and the different types of validation that can be used in ASP.NET. I would highly recommend this article to anyone who is looking to improve the accuracy of their applications.