CompareValidator for ASP. NET verification controls

Source: Internet
Author: User

You should be familiar with CompareValidator. It is used to compare the values entered in the two input boxes. The most common one is to check whether the passwords entered twice are consistent during user registration. I believe that anyone on the Internet has ever met me! Let's take a look at his common attributes:

Attribute
Description
BackColor
Background color.
ControlToCompare
The input control to be compared with the verified input control.
ControlToValidate
ID of the input control to be verified.
Display
Verify the display behavior of error messages in the control.
Valid values:
· None verification messages are never displayed inline.
· Static: Allocate space for displaying verification messages in the page layout.
· If the verification fails, Dynamic dynamically adds the space used to display the verification message to the page.
EnableClientScript
Boolean value that specifies whether to enable client verification. True indicates enabled, and false indicates disabled.
Enabled
Boolean value that specifies whether to enable the verification control. True indicates enabled, and false indicates disabled.
ErrorMessage
Text displayed in the ValidationSummary control when verification fails.
Note: If the Text property is not set, the Text is displayed in the verification control.
ForeColor
The foreground color of the widget. That is, the font color of the error message.
Id
The unique ID of the control.
IsValid
Boolean value indicating whether the input control specified by ControlToValidate has passed verification. True indicates pass, and false indicates fail.
Operator
The type of the compare operation to be executed.
OPERATOR:
· Equal equals
· GreaterThan is greater
· GreaterThanEqual greater than or equal
· LessThan is smaller
· LessThanEqual is less than or equal
· NotEqual is not equal
· DataTypeCheck verifies the data type (if the specified type is integer, the input character cannot pass verification)
Runat
The specified control is a server control. Must be set to "server ".
Text
Message displayed when verification fails.
Type
Specifies the Data Type of the value to be compared.
Types:
· Currency
· Date
· Double
· Integer
· String
ValueToCompare
A constant value, which must be compared with the value entered by the user to the verified input control.


The following is the actual application of CompareValidator:
 
 
[Html]
<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "password comparison. aspx. cs" Inherits = "WebApplication1. password comparison" %>
 
 
<! DOCTYPE html>
 
 
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "Head1" runat = "server">
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> password comparison </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
 
 
<Asp: Label ID = "lbPWD1" runat = "server" Text = "enter the password:"> </asp: Label>
<Asp: TextBox ID = "txtPWD1" runat = "server" TextMode = "Password"> </asp: TextBox>
 
 
<Br/>
<Br/>
 
 
<Asp: Label ID = "lbPWD2" runat = "server" Text = "Confirm Password:"> </asp: Label>
<Asp: TextBox ID = "txtPWD2" runat = "server" TextMode = "Password"> </asp: TextBox>
<Asp: compareValidator ID = "CompareValidator1" ForeColor = "Red" runat = "server" ErrorMessage = "two different password inputs" ControlToValidate = "txtPWD1" ControlToCompare = "txtPWD2"> </asp: compareValidator>
 
 
<Br/>
<Br/>
 
 
<Asp: Button ID = "btnSubmit" runat = "server" Text = "Submit"/>
 
 
</Form>
</Body>
</Html>
  
When the two passwords are inconsistent, the following prompt is displayed (of course, the prompt message is defined by yourself ):


In addition, if the two boxes are all entered with spaces and the numbers are different, they will be considered the same, because CompareValidator treats the content with all spaces as nothing. However, if the actual content is entered, no matter where the space is added, an error will be reported if the content of the two boxes is different. CompareValidator does not remove spaces at the beginning and end by default. Note This.
 
Although CompareValidator differs from js in terms of resource consumption and user experience, it is easy to use and is suitable for beginners. I will introduce the js method later.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.