Table of contents
1.
Introduction
2.
Validation Controls in ASP.NET
3.
Compare Validator
4.
Compare Validator Control
5.
Compare Validator Example
6.
Frequently Asked Questions
7.
Key Takeaways
Last Updated: Mar 27, 2024

ASP.NET Compare Validator

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Validation plays an essential role in web applications. The user’s input must be validated before sending it across different application layers. The validation controls implement presentation logic to validate user input,  data format, data type, and date range. 

The validation is further divided into two categories:

  1. Client-side validation: This validation is convenient for users as they get instant feedback. The main advantage of this validation is that it prevents the page from being postback to the server until the client-side validation is executed successfully. It is dependent on the browser and scripting language.
     
  2. Server-side validation: This validation is preferable from a developer’s point of view as it will not fail. It is not dependent on the browser or scripting language. 

 

ASP.NET validation ensures both client-side and server-side validation. First, it works on the client-side validation and then on the server-side validation. The server validation will always run whether client validation is executed or not. Hence, we have safety of validation check.

Validation Controls in ASP.NET

A crucial aspect of creating ASP.NET web pages for user input is to check that the user’s entered input is valid. The ASP.NET provides a set of validation controls that provide an easy yet powerful way to check for errors and displays messages to the user.

Following are the six types of validation controls in ASP.NET along with their working:  

  1. RequiredFieldValidation Control: It makes an input control a required field.
     
  2. CompareValidator Control: It compares the value of one input to another. It can also be used to fix a value.
     
  3. RangeValidator Control: It is used to check whether the input entered by the user falls under the required criteria.
     
  4. RegularExpressionValidator Control: It ensures that the value of an input control matches the specified pattern.
     
  5. CustomValidator Control: This allows writing a method to validate the entered value.
     
  6. ValidationSummary: It displays a report of all the validation errors that occur on a web page.

 

This blog will discuss one of the validation controls, i.e., compare validator with a few examples.

Compare Validator

The CompareValidator control allows comparisons of entered data in an input control with a constant value or another value in a different control.

It is commonly used when we need to confirm the entered password by the user at the registration time. The data is always case-sensitive.

Syntax:

<asp:CompareValidator ID=“CompareValidator1“ runat=“server“ ErrorMessage=“CompareValidator“></asp:CompareValidator>

Compare Validator Control

This validator is used to evaluate the value of an input control against another based on a specified operator. Note that no validation will be performed if the input field is empty.

Let’s have a look at the Compare Validator properties:

  1. Accesskey: It is used to set the keyboard’s shortcut.
     
  2. TabIndex: It is used to tab order of the control.
     
  3. BackColor: It is used to set the background color of the control.
     
  4. BorderColor: It is used to set the border color of the control.
     
  5. BorderWidth: It is used to set the width of the border.
     
  6. Font: It is used to set the control text’s font.
     
  7. ForeColor: It is used to set the control’s text color.
     
  8. Text: It is used to set the text to be displayed for the control.
     
  9. ToolTip: It displays the hovered control text by the mouse.
     
  10. Visible: It sets the control’s visibility on the form.
     
  11. Height: It is used to set the height of the control.
     
  12. Width: It is used to set the width of the control.
     
  13. ControlToCompare: It extracts the ID of the control for comparison.
     
  14. ControlToValidate: It extracts the ID of the control for validation.
     
  15. ErrorMessage: It displays an error message in case of validation failure.
     
  16. Operator: It is used to set the comparison operator.

Compare Validator Example

Consider the following example that will help you understand the concept of compare validator better: 

Compare.aspx

<%@ Page Language="C#"%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        Small Number:<br />
        <asp:TextBox runat="server" id="txtSmallNumber" /><br />
        <br />
        Large Number:<br />
        <asp:TextBox runat="server" id="txtLargeNumber" /><br />
        <asp:CompareValidator runat="server" id="Numbers" controltovalidate="txtSmallNumber" controltocompare="txtLargeNumber" operator="LessThan" type="Integer" errormessage=" The first number should be smaller" /><br />
    </body>
</html>

 

Output:

Frequently Asked Questions

  1. What is the difference between servers.Transfer and Response.Redirect?
    Server.Transfer: In server.transfer page processing transfers from one page to another without making a round-trip back to the client’s browser. This provides a faster response with less overhead on the server. The client’s URL history list or current URL server does not update in the case of the server.Transfer.
    Response.Redirect: It redirects the user’s browser to another page. It goes back to the client, where the client’s browser is redirected to the new page. The user’s browser history is updated that reflect the new address.
     
  2. What do you understand by Cross Page Posting?
    When we press the submit button on a web page, the page post the data to the same page. The technique in which the user posts the data to different pages is known as Cross Page Posting. It can be achieved by setting the POSTBACK URL property of the button that causes the postback. 
     
  3. Explain Role-Based Security?
    Role-Based Security is used to implement the security based on roles assigned to the user groups in the organization. A developer can allow or deny others based on their role in the organization. Windows defines several built-in groups, including administrators, users, and guests.
     
<AUTHORIZATION>< authorization >
< allow roles="Domain_Name\Administrators" / >
< deny users="*"  / >                            
< /authorization >

Key Takeaways

This blog taught us about ASP.Net Compare Validator. The user’s input must be validated before sending it across different application layers. The CompareValidator control allows comparisons of entered data in an input control with a constant value or another value in a different control. 

If you are a beginner and are interested in learning other fields related to computer subjects such as web development, or competitive programming, you can follow our guided path to get a good grip on such concepts.

Live masterclass