CompareValidator for number, text and password : CompareValidator « Validation « ASP.NET Tutorial






<%@ 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>








8.2.CompareValidator
8.2.1.CompareValidator performs three different types of validations
8.2.2.CompareValidator performs a comparison against a fixed value.
8.2.3.CompareValidator compares the value of one form field against another form field.
8.2.4.asp:CompareValidator, Type='String', Operator='NotEqual' and ErrorMessage
8.2.5.What happens to asp:CompareValidator in the client side
8.2.6.Set DeleteParameters, InsertParameters and UpdateParameters for asp:SqlDataSource
8.2.7.Using the CompareValidator to test values against other control values
8.2.8.Using the CompareValidator to validate against constants
8.2.9.To specify the data types: Currency, Date, Double, Integer, String
8.2.10.Making comparisons with the CompareValidator control
8.2.11.CompareValidator for number, text and password
8.2.12.Disable Client Validation
8.2.13.Use CompareValidator to check password field and confirm password field
8.2.14.CompareValidator with age field
8.2.15.Check birthday