Year End Sale: Get Upto 40% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now
Types of Validation Control in Asp.Net

Types of Validation Control in Asp.Net

20 Jun 2024
Beginner
10.8K Views
9 min read
Learn with an interactive course and practical hands-on labs

Free ASP.NET Core Course

Validation Control in Asp.Net

ASP.NET enables programmers to create dynamic web pages and design robust and quality websites. ASP.NET validation controls validate the user input data to ensure it is not useless, unauthenticated, or contradictory. It provides a way to check the accuracy and validity of user input before the web application processes it.

In this .NET tutorial, we will explore more about Validation Controls, andTypes of Validation Control in Asp.Net. For the implementation of learned concepts, consider our ASP.NET Core Certification Training. Also If you are preparing for an Asp.Net interview the following articles will help you to crack it.

Use of Validation Controls

  • It is used to validate user input value.
  • It is used to help in implementing presentation logic Which is a layer of the web application.
  • Validation Controls are used in data formatting, data ranging, and data type for validation.

Types of Validation Controls in Asp.Net

Types of Validation Controls

1. RequiredFieldValidator

  • It is also known as an elementary validation control.
  • The form consists of mandatory fields to be filled.
  • If the users or clients want to proceed with the form, they will have to mandatorily fill these fields, RequiredFieldValidator is used when such fields are not left empty.
  • It checks that there must be some value added within the control.
  • Let's discuss some important properties of RequiredFieldValidators such as Initial value, ControlToValidate, and Text.
  • Initial value is shown by default to guide the users or client on how the value must be added, hence it is especially used for a drop-down list.
  • The second property is the ControlToValidate property, It is used to set the field of the text box for validation purposes.
  • And the last property is "Text", It is used to set the text value for the validation.

Syntax

Let's see RequiredFieldValidator syntax
<asp:RequiredFieldValidator ID="rfvcandidate" 
 runat="server" ControlToValidate ="ddlcandidate"
 ErrorMessage="Welcome to Scholarhat" 
 InitialValue="Welcome to Scholarhat">
</asp:RequiredFieldValidator>    

2. RangeValidator

  • As its name, It is used to check whether the value of the input control is inside a particular range or not.
  • RangeValidator control is used when it comes to getting inputs such as Age, Date of Birth, or mobile numbers from the user.
  • Let's discuss some important properties of RangeValidator control such as Minimum Value, Maximum Value, ControlToValidate, and Type.
  • Minimum Value is used when the programmer holds the valid range’s minimum value.
  • The maximum value is used when the programmer holds a valid range’s maximum value.
  • ControlToValidate allows the programmer to set the specific control that needs to be validated.
  • Type is used to set after the above properties if needed.
  • This validator can compare the following data types:

RangeValidator

Syntax

Let's see RangeValidator syntax
<asp:RangeValidator ID="rvclass" runat="server" ControlToValidate="txtclass" 
   ErrorMessage="Enter the class (3 - 12)" MaximumValue="12" 
   MinimumValue="3" Type="Integer">   

3. RegularExpressionValidator

  • RegularExpressionValidator also known as Regex.
  • It is a control that manages various patterns which clearly define the format of the text.
  • Hence if the text that has been added is in the same format then the RegularExpressionValidator will return true or else false.
  • It is used to validate input fields such as email, phone, Zip code, etc.

Syntax

Let's see RegularExpressionValidator syntax
<asp:RegularExpressionValidator ID="string" runat="server" ErrorMessage="string"
 ValidationExpression="string" ValidationGroup="string">
</asp:RegularExpressionValidator>      

4. CompareValidator Control

  • It compares the value of one control with either a fixed value or a value in another control.
  • There are three properties included in this validator such as ControlToCompare, ValueToCompare, and Operator.
  • ControlToCompare holds the ControlToValidate ID of another form.
  • Then, The value of both the form fields is then compared.
  • ValueToCompare is a fixed value with which the comparison has to be made.
  • Operator is the type of comparison and its attributes are:

 CompareValidator Control

Syntax

Let's see the CompareValidator Control syntax
<asp:CompareValidator ID="CompareValidator1" runat="server" 
 ErrorMessage="CompareValidator">
</asp:CompareValidator>       

5. CustomValidator Control

  • It is used to customize and implement data validation as per the requirements and conditions that occur.
  • The developers use CustomValidationFor instance when they want to check whether the entered number is even or odd.
  • Some of the major properties of custom validators are ClientValidationFunction and ValidateEmptyText.
  • ClientValidationFunction is used to set the name of the function in the custom client-side scripting languages such as javascript or VBScript for validation purposes.
  • ValidateEmptyText is used to set a Boolean value that checks whether the empty text is valid or not.

Syntax

Let's see the CustomValidator Control syntax
<asp:CustomValidator ID="CustomValidator1" runat="server" 
ClientValidationFunction=.cvf_func. ErrorMessage="CustomValidator">
</asp:CustomValidator>     

6. ValidationSummary Control

  • It is a control that is used to show error messages.
  • It also collects every type of validation control error message and is used by the other validation controls on a web page.
  • It then shows the error message on the screen.
  • The properties of validation summary control are Forecolor, DisplayMode,and HeaderText
  • Forecolor is used to set the foreground color.
  • DisplayMode is used to set the display mode of the control.
  • HeaderText is used to set at the top of the summary.

Syntax

Let's see the ValidationSummary Control syntax
<asp:ValidationSummary ID="ValidationSummary1" runat="server"  DisplayMode = "BulletPoints" ShowSummary = "true" HeaderText="Errors:" />      

Validation Controls In a Nutshell

Validation Controls

Conclusion:

So in this article, we have learned about Types of Validation Controls in Asp.Net. I hope you enjoyed learning these concepts while programming with .Net. Feel free to ask any questions from your side. Your valuable feedback or comments about this article are always welcome. Consider our .NET Certification Training to learn .net from scratch.

FAQs

The RequiredFieldValidator is a fundamental validation control in ASP.Net that allows developers to check if user input is null or empty.

An ASP.NET control is a . NET class that executes on the server and renders certain content to the browser.

It is used to validate the user input data, such as data format, data type, and date range.
Share Article
About Author
Shailesh Kumar (Azure Architect and Mentor)

Shailesh Kumar is a qualified Engineer with 21 Years of IT experience and strong infrastructure management expertise. Delivered successful corporate training to Fortune CMM Level 5 Clients like TCS, Wipro, IBM, Cognizant, Atos, ETC. He is certified in Azure Administration, Azure DevOps, and Microsoft Certified Trainer.

Accept cookies & close this