Latest web development tutorials

ASP.NET CompareValidator Controls

Validation Server Controls Validation Server Controls

Definition and Usage

CompareValidator control is used to enter a value or a constant value of the control value of another input control for comparison.

Note: If theinput control is empty, it will not call any validation functions, and verification will succeed. Use the RequiredFieldValidator control to make the required field (required).


Attributes

属性 描述
BackColor CompareValidator 控件的背景颜色。
ControlToCompare 要与所验证的控件进行比较的控件名称。
ControlToValidate 要验证的控件的 id。
Display 验证控件的显示行为。合法值有:
  • None - 控件不显示。仅用于 ValidationSummary 控件中显示错误消息。
  • Static - 如果验证失败,控件显示错误消息。即使输入通过验证,也在页面上预留显示消息的空间,即用于显示消息的空间是预先分配好的。
  • Dynamic - 如果验证失败,控件显示错误消息。如果输入通过验证,页面上不预留显示消息的空间,即用于显示消息的空间是动态添加的。
EnableClientScript 布尔值,规定是否启用客户端验证。
Enabled 布尔值,规定是否启用验证控件。
ErrorMessage 当验证失败时,在 ValidationSummary 控件中显示的文本。 注释:如果未设置 Text 属性,文本也会显示在验证控件中。
ForeColor 控件的前景颜色。
id 控件的唯一 id。
IsValid 布尔值,指示由 ControlToValidate 指定的控件是否通过验证。
Operator 要执行的比较操作的类型。运算符有:
  • Equal
  • GreaterThan
  • GreaterThanEqual
  • LessThan
  • LessThanEqual
  • NotEqual
  • DataTypeCheck
runat 规定控件是服务器控件。必须设置为 "server"。
Text 当验证失败时显示的消息。
Type 规定要对比的值的数据类型。类型有:
  • Currency
  • Date
  • Double
  • Integer
  • String
ValueToCompare 一个常数值,该值要与由用户输入到所验证的输入控件中的值进行比较。


Examples

CompareValidator
In this example, we declare two TextBox controls in an .aspx file, a Button control, and a CompareValidator control. If the validation fails, the yellow background red letters "Validation Failed!" Text in CompareValidator control.

CompareValidator 2
In this example, we declare in an .aspx file, two TextBox controls, a ListBox control, a Button control, and a CompareValidator control. check_operator () function to select ListBox control operator set CompareValidator control operator, then CompareValidator control verification. If the validation fails, the yellow background red letters "Validation Failed!" Text in CompareValidator control.


Validation Server Controls Validation Server Controls