23
NovTypes of Validation Control in Asp.Net
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.
- Top 50 ASP.NET Interview Questions and Answers
- Top 50 ASP.NET Core Interview Questions and Answers for 2024
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
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
<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:
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
<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:
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
<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
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
DisplayMode = "BulletPoints" ShowSummary = "true" HeaderText="Errors:" />
Validation Controls In a Nutshell
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.