ASP.NET Tutorial/Validation/CompareValidator — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 11:57, 26 мая 2010

asp:CompareValidator, Type="String", Operator="NotEqual" and ErrorMessage

<html><body>
   <form runat="server">
      <asp:Label id="lblMessage" runat="server" /><br>
      <asp:Panel id="Panel1" runat="server">
         <table>
         <tr>
            <td>
               First and last name:
            </td>
            <td>
               <asp:TextBox id="tbFName" runat="server" />
               <asp:TextBox id="tbLName" runat="server" /><br>
               <asp:RequiredFieldValidator runat="server" 
                  ControlToValidate="tbFName" 
                  ErrorMessage="First name required"/><br>
               <asp:RequiredFieldValidator runat="server" 
                  ControlToValidate="tbLName" 
                  ErrorMessage="Last name required"/><br>
               <asp:CompareValidator runat="server"
                  ControlToValidate="tbFName"
                  ControlToCompare="tbLName"
                  Type="String"
                  Operator="NotEqual"
                  ErrorMessage="First and last name cannot be the same" />
            </td>
         </tr>
         <tr>
            <td>
               <asp:Button id="Submit" runat="server" 
                  text="Add" />
            </td>
         </tr>
         </table>
      </asp:Panel>
   </form>
</body></html>


Check birthday

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Login" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Simple Login Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        
          Username:<br />
          <asp:TextBox id="usernameTextBox" runat="server" />
          <asp:RequiredFieldValidator id="usernameReq"
              runat="server"
              ControlToValidate="usernameTextBox"
              ErrorMessage="Username is required!"
              SetFocusOnError="True" />
        
        
          Password and Confirmation:<br />
          <asp:TextBox id="passwordTextBox" 
              runat="server"
              TextMode="Password" />
          <asp:RequiredFieldValidator id="passwordReq"
              runat="server"
              ControlToValidate="passwordTextBox"
              ErrorMessage="Password is required!"
              SetFocusOnError="True" 
              Display="Dynamic" />
          <asp:TextBox id="confirmPasswordTextBox" runat="server"
              TextMode="Password" />
          <asp:RequiredFieldValidator id="confirmPasswordReq"
              runat="server" ControlToValidate="confirmPasswordTextBox"
              ErrorMessage="Password confirmation is required!"
              SetFocusOnError="True" Display="Dynamic" />
          <asp:CompareValidator id="comparePasswords" runat="server"
              ControlToCompare="passwordTextBox"
              ControlToValidate="confirmPasswordTextBox"
              ErrorMessage="Your passwords do not match up!"
              Display="Dynamic" />
        
        
          Birth Date:<br />
          <asp:TextBox id="birthDateTextBox" runat="server" />
          <asp:CompareValidator id="birthDateCheck" runat="server"
              Operator="DataTypeCheck" Type="Date"
              ControlToValidate="birthDateTextBox" 
              ErrorMessage="You must enter the date in a valid format!"
              SetFocusOnError="True" 
              Display="Dynamic" />
        
        
          <asp:Button id="submitButton" runat="server"
              Text="Submit" OnClick="submitButton_Click" />
        
    </div>
    </form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Login : System.Web.UI.Page
{
    protected void submitButton_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            submitButton.Text = "Valid";
        }
        else
        {
            submitButton.Text = "Invalid!";
        }
    }
}


CompareValidator compares the value of one form field against another form field.

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Show Compare Values</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label
        id="lblStartDate"
        Text="Start Date:"
        Runat="server" />
    <asp:TextBox
        id="txtStartDate"
        Runat="server" />
    <br /><br />
    <asp:Label
        id="lblEndDate"
        Text="End Date:"
        Runat="server" />
    <asp:TextBox
        id="txtEndDate"
        Runat="server" />
    <asp:CompareValidator
        id="cmpDate"
        Text="(End date must be greater than start date)"
        ControlToValidate="txtEndDate"
        ControlToCompare="txtStartDate"
        Type="Date"
        Operator="GreaterThan"
        Runat="server" />
    <asp:Button
        id="btnSubmit"
        Text="Submit"
        Runat="server" />
    </div>
    </form>
</body>
</html>


CompareValidator for number, text and password

<%@ Page Language="C#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Using CompareValidator Control</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      
      Age:<br />
      <asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
      <asp:CompareValidator ID="compAge" runat="server" 
         ControlToValidate="txtAge" 
         ValueToCompare="18"
         Operator="LessThanEqual"
         SetFocusOnError="true"
         Text="You are too old to view this site" />
      
        
        Sales Date:<br />
        <asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
        <asp:CompareValidator ID="compDate" runat="server" 
           ControlToValidate="txtDate" 
           Operator="DataTypeCheck"
           Type="Date"
           Text="Enter a valid date" />
        
              
        
        Quantity:<br />
        <asp:TextBox ID="txtQuantity" runat="server"></asp:TextBox>
           <asp:CompareValidator ID="compQuantity" runat="server" 
           ControlToValidate="txtQuantity" 
           Operator="DataTypeCheck"
           Type="Integer"
           Text="Enter a valid whole number" />   
        
        
        
        Enter Password:<br />
        <asp:TextBox ID="txtPass1" runat="server" TextMode="password"></asp:TextBox>
        
        
        Reenter Password:<br />
        <asp:TextBox ID="txtPass2" runat="server" TextMode="password"></asp:TextBox>
        
        
        <asp:CompareValidator ID="compPass" runat="server" 
           ControlToValidate="txtPass2" 
           Operator="Equal"
           ControlToCompare="txtPass1"
           Text="Passwords must match" />
        
      <asp:Button ID="btnSubmit" Text="Click this to test validation" runat="server" />
      
    </div>
    </form>
</body>
</html>


CompareValidator performs a comparison against a fixed value.

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    void Page_Load()
    {
        cmpDate.ValueToCompare = DateTime.Now.ToString("d");
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Show Fixed Value</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label
        id="lblDate"
        Text="Date:"
        AssociatedControlID="txtDate"
        Runat="server" />
    <asp:TextBox
        id="txtDate"
        Runat="server" />
    <asp:CompareValidator
        id="cmpDate"
        Text="(Date must be greater than now)"
        ControlToValidate="txtDate"
        Type="Date"
        Operator="GreaterThan"
        Runat="server" />
    <br /><br />
    <asp:Button
        id="btnSubmit"
        Text="Submit"
        Runat="server" />
    </div>
    </form>
</body>
</html>


CompareValidator performs three different types of validations

A data type check
Compare the value against a fixed value
Compare the value against another

Important properties
ControlToValidate: The ID of the form field being validated.
Text:              The error message.
Type:              The type of value being compared. 
                   Possible values are String, Integer, Double, Date, and Currency.
Operator:          The type of comparison to perform. 
                   Possible values are DataTypeCheck, Equal, GreaterThan, GreaterThanEqual, LessThan, LessThanEqual, and NotEqual.
ValueToCompare:    The fixed value.
ControlToCompare:  The ID of a control against which to compare.

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Show Data Type Check</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label
        id="lblBirthDate"
        Text="Birth Date:"
        AssociatedControlID="txtBirthDate"
        Runat="server" />
    <asp:TextBox
        id="txtBirthDate"
        Runat="server" />
    <asp:CompareValidator
        id="cmpBirthDate"
        Text="(Invalid Date)"
        ControlToValidate="txtBirthDate"
        Type="Date"
        Operator="DataTypeCheck"
        Runat="server" />
    <br /><br />
    <asp:Button
        id="btnSubmit"
        Text="Submit"
        Runat="server" />
    </div>
    </form>
</body>
</html>


CompareValidator with age field

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Login" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Simple Login Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        
          Username:<br />
          <asp:TextBox id="usernameTextBox" runat="server" />
          <asp:RequiredFieldValidator id="usernameReq"
              runat="server"
              ControlToValidate="usernameTextBox"
              ErrorMessage="Username is required!"
              SetFocusOnError="True" />
        
        
          Password and Confirmation:<br />
          <asp:TextBox id="passwordTextBox" runat="server"
              TextMode="Password" />
          <asp:RequiredFieldValidator id="passwordReq"
              runat="server"
              ControlToValidate="passwordTextBox"
              ErrorMessage="Password is required!"
              SetFocusOnError="True" Display="Dynamic" />
          <asp:TextBox id="confirmPasswordTextBox" runat="server"
              TextMode="Password" />
          <asp:RequiredFieldValidator id="confirmPasswordReq"
              runat="server" 
              ControlToValidate="confirmPasswordTextBox"
              ErrorMessage="Password confirmation is required!"
              SetFocusOnError="True" 
              Display="Dynamic" />
          <asp:CompareValidator id="comparePasswords" runat="server"
              ControlToCompare="passwordTextBox"
              ControlToValidate="confirmPasswordTextBox"
              ErrorMessage="Your passwords do not match up!"
              Display="Dynamic" />
        
        <!-- Age -->
        
          Age:<br />
          <asp:TextBox id="ageTextBox" runat="server" />
          <asp:RequiredFieldValidator id="ageReq" runat="server"
              ControlToValidate="ageTextBox"
              ErrorMessage="Age is required!"
              SetFocusOnError="True" Display="Dynamic" />
          <asp:CompareValidator id="ageCheck" runat="server"
              Operator="GreaterThan" 
              Type="Integer"
              ControlToValidate="ageTextBox" 
              ValueToCompare="15"
              ErrorMessage="You must be 16 years or older to log in" />
        
        
          <asp:Button id="submitButton" runat="server"
              Text="Submit" 
              OnClick="submitButton_Click" />
        
    </div>
    </form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Login : System.Web.UI.Page
{
    protected void submitButton_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            submitButton.Text = "Valid";
        }
        else
        {
            submitButton.Text = "Invalid!";
        }
    }
}


Disable Client Validation

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="DisableClientValidation" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    
    Quantity:<br />
    <asp:TextBox ID="txtQuantity" runat="server"></asp:TextBox>
       <asp:CompareValidator ID="compQuantity" 
                             runat="server" 
                             ControlToValidate="txtQuantity" 
                             Operator="DataTypeCheck"
                             Type="Integer"
                             EnableClientScript="false"
                             Text="Enter a valid whole number" />   
    
    
    <asp:Button ID="btnSubmit" Text="Click this to test validation" runat="server" OnClick="btnSubmit_Click" />
    
    <asp:Label ID="labContent" runat="server" />      
    </form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class DisableClientValidation : System.Web.UI.Page
{
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
       if (IsValid)
       {
          int quantity = Convert.ToInt32(txtQuantity.Text);
          int unitCost = 5;
          int price = quantity * unitCost;
          labContent.Text = "Price for order is $" + price;
       }   
    }
}


Making comparisons with the CompareValidator control

<%@ Page Language="VB" %>
<script runat="server">
    Protected Sub Button1_Click(sender As Object, e As EventArgs)
       Label1.Text = "Passwords match"
    End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>CompareFieldValidator</title>
</head>
<body>
    <form id="Form1" runat="server">
        Age:
        <asp:TextBox ID="TextBox1" Runat="server"></asp:TextBox>
          &nbsp;
        <asp:CompareValidator ID="CompareValidator1" 
                              Runat="server"
                              Operator="GreaterThan" 
                              ValueToCompare="18"
                              ControlToValidate="TextBox1"
                              ErrorMessage="You must be older than 18 to join" 
                              Type="Integer">
        </asp:CompareValidator>
        
            <asp:Button ID="Button1" OnClick="Button1_Click" 
             Runat="server" Text="Login"></asp:Button>
        
        
            <asp:Label ID="Label1" Runat="server"></asp:Label>
        
    </form>
</body>
</html>


Set DeleteParameters, InsertParameters and UpdateParameters for asp:SqlDataSource

<%@ Page Language="VB" AutoEventWireup="false"%>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
            DeleteCommand="DELETE FROM [Books] WHERE [BookID] = @BookID" 
            InsertCommand="INSERT INTO [Books] ([Title], [Author], [YearPublished], [Price], [LastReadOn], [PageCount]) VALUES (@Title, @Author, @YearPublished, @Price, @LastReadOn, @PageCount)"
            SelectCommand="SELECT * FROM [Books]" 
            UpdateCommand="UPDATE [Books] SET [Title] = @Title, [Author] = @Author, [YearPublished] = @YearPublished, [Price] = @Price, [LastReadOn] = @LastReadOn, [PageCount] = @PageCount WHERE [BookID] = @BookID">
            <DeleteParameters>
                <asp:Parameter Name="BookID" Type="Int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="Title" Type="String" />
                <asp:Parameter Name="Author" Type="String" />
                <asp:Parameter Name="YearPublished" Type="Int32" />
                <asp:Parameter Name="Price" Type="Decimal" />
                <asp:Parameter Name="LastReadOn" Type="DateTime" />
                <asp:Parameter Name="PageCount" Type="Int32" />
                <asp:Parameter Name="BookID" Type="Int32" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="Title" Type="String" />
                <asp:Parameter Name="Author" Type="String" />
                <asp:Parameter Name="YearPublished" Type="Int32" />
                <asp:Parameter Name="Price" Type="Decimal" />
                <asp:Parameter Name="LastReadOn" Type="DateTime" />
                <asp:Parameter Name="PageCount" Type="Int32" />
            </InsertParameters>
        </asp:SqlDataSource>
    
    </div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White"
            DataSourceID="SqlDataSource1">
            <FooterStyle BackColor="#CCCC99" />
            <Columns>
                <asp:CommandField ShowDeleteButton="True" ButtonType="Button" DeleteText="Delete Book" />
                <asp:BoundField DataField="BookID" HeaderText="BookID" InsertVisible="False" ReadOnly="True"
                    SortExpression="BookID" />
                <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                <asp:BoundField DataField="Author" HeaderText="Author" SortExpression="Author" />
                <asp:BoundField DataField="YearPublished" HeaderText="YearPublished" SortExpression="YearPublished" />
                <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
                <asp:BoundField DataField="LastReadOn" HeaderText="LastReadOn" SortExpression="LastReadOn" />
                <asp:BoundField DataField="PageCount" HeaderText="PageCount" SortExpression="PageCount" />
            </Columns>
            <RowStyle BackColor="#F7F7DE" />
            <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
            <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
    </form>
</body>
</html>
File: Web.config
<?xml version="1.0"?>
<configuration>
    <connectionStrings>
        <add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MyFirstDatabase.mdf;Integrated Security=True;User Instance=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>


To specify the data types: Currency, Date, Double, Integer, String

<%@ Page Language="VB" %>
<script runat="server">
    Protected Sub Button1_Click(sender As Object, e As EventArgs)
       Label1.Text = "Passwords match"
    End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>CompareFieldValidator</title>
</head>
<body>
    <form id="Form1" runat="server">
        Age:
        <asp:TextBox ID="TextBox1" Runat="server" MaxLength="3"/>
        <asp:CompareValidator ID="CompareValidator1" 
                              Runat="server"
                              ErrorMessage="You must enter a number"
                              ControlToValidate="TextBox1" 
                              Type="Integer"
                              Operator="DataTypeCheck"/>
        
            <asp:Button ID="Button1" OnClick="Button1_Click" 
             Runat="server" Text="Login"></asp:Button>
        
        
            <asp:Label ID="Label1" Runat="server"></asp:Label>
        
    </form>
</body>
</html>


Use CompareValidator to check password field and confirm password field

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Login" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Simple Login Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        
          Username:<br />
          <asp:TextBox id="usernameTextBox" runat="server" />
          <asp:RequiredFieldValidator id="usernameReq"
              runat="server"
              ControlToValidate="usernameTextBox"
              ErrorMessage="Username is required!"
              SetFocusOnError="True" />
        
        
          Password and Confirmation:<br />
          <asp:TextBox id="passwordTextBox" runat="server"
              TextMode="Password" />
          <asp:RequiredFieldValidator id="passwordReq"
              runat="server"
              ControlToValidate="passwordTextBox"
              ErrorMessage="Password is required!"
              SetFocusOnError="True" Display="Dynamic" />
          <asp:TextBox id="confirmPasswordTextBox" runat="server"
              TextMode="Password" />
          <asp:RequiredFieldValidator id="confirmPasswordReq"
              runat="server" 
              ControlToValidate="confirmPasswordTextBox"
              ErrorMessage="Password confirmation is required!"
              SetFocusOnError="True" 
              Display="Dynamic" />
          <asp:CompareValidator id="comparePasswords" 
              runat="server"
              ControlToCompare="passwordTextBox"
              ControlToValidate="confirmPasswordTextBox"
              ErrorMessage="Your passwords do not match up!"
              Display="Dynamic" />
        
        
          <asp:Button id="submitButton" 
              runat="server"
              Text="Submit" 
              OnClick="submitButton_Click" />
        
    </div>
    </form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Login : System.Web.UI.Page
{
    protected void submitButton_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            submitButton.Text = "Valid";
        }
        else
        {
            submitButton.Text = "Invalid!";
        }
    }
}


Using the CompareValidator to test values against other control values

<%@ Page Language="VB" %>
<script runat="server">
    Protected Sub Button1_Click(sender As Object, e As EventArgs)
       Label1.Text = "Passwords match"
    End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>CompareFieldValidator</title>
</head>
<body>
    <form id="Form1" runat="server">
        
            Password<br>
            <asp:TextBox ID="TextBox1" Runat="server" 
             TextMode="Password"></asp:TextBox>
             &nbsp;
            <asp:CompareValidator ID="CompareValidator1" 
             Runat="server" ErrorMessage="Passwords do not match!" 
             ControlToValidate="TextBox2" 
             ControlToCompare="TextBox1"></asp:CompareValidator>
        
        
            Confirm Password<br>
            <asp:TextBox ID="TextBox2" Runat="server" 
             TextMode="Password"></asp:TextBox>
        
        
            <asp:Button ID="Button1" OnClick="Button1_Click" 
             Runat="server" Text="Login"></asp:Button>
        
        
            <asp:Label ID="Label1" Runat="server"></asp:Label>
        
    </form>
</body>
</html>


Using the CompareValidator to validate against constants

<%@ Page Language="VB" %>
<script runat="server">
    Protected Sub Button1_Click(sender As Object, e As EventArgs)
       Label1.Text = "Passwords match"
    End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>CompareFieldValidator</title>
</head>
<body>
    <form id="Form1" runat="server">
        Age:
        <asp:TextBox ID="TextBox1" Runat="server" MaxLength="3">
        </asp:TextBox>
        <asp:CompareValidator ID="CompareValidator1" 
                              Runat="server"
                              ErrorMessage="You must enter a number"
                              ControlToValidate="TextBox1" 
                              Type="Integer"
                              Operator="DataTypeCheck"/>
        
            <asp:Button ID="Button1" OnClick="Button1_Click" 
             Runat="server" Text="Login"></asp:Button>
        
        
            <asp:Label ID="Label1" Runat="server"></asp:Label>
        
    </form>
</body>
</html>


What happens to asp:CompareValidator in the client side

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="MyPage" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Total number of children:
        <asp:TextBox ID="totalChildren" runat="server" Columns="4"></asp:TextBox>
        <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="totalChildren"
            ErrorMessage="The total number of children must be a whole number greater than or equal to 0"
            Operator="GreaterThanEqual" Type="Integer" ValueToCompare="0"></asp:CompareValidator><br />
        <br />
        <asp:Button ID="btnSubmit" runat="server" Text="Click Me" />
        <br />
        <br />
        <asp:Label ID="results" runat="server"></asp:Label></div>
    </form>
</body>
</html>
File: Default.aspx.vb

Partial Class MyPage
    Inherits System.Web.UI.Page
    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        If Page.IsValid Then
            results.Text = "Input is valid..."
        Else
            results.Text = "Input is <b>not</b> valid..."
        End If
    End Sub
End Class