. NET-based verification control

Source: Internet
Author: User

1. RequiredFieldValidator: A non-empty verification control
Main attributes:
ControlToValidate: ID of the control to be verified
ErrorMessage: the message displayed when the verified control is invalid.
InitialValue: the initial value of the field to be verified, rarely used

 

2. CompareValidator: Comparison verification control
Main attributes:
ControlToCompare: Control ID used for comparison
ControlToValidate: ID of the control to be verified
ErrorMessage: the message displayed when the verified control is invalid.
Operator: Compare values. The default value is Equal (=)
Type: the data Type of the value to be compared.

Operator attributes

Description

Equal

The value of the verified input control is equal to the value or constant value of another control.

NotEqual

The value of the verified input control is not equal to the value or constant value of another control.

GreaterThan

The value of the verified input control is greater than or equal to the value or constant value of another control.

GreaterThanEqual

The value of the verified input control is greater than or equal to the value or constant value of other controls.

LessThan

The value of the verified input control is less than or equal to the value or constant value of another control.

LessThanEqual

The value of the verified input control is less than or equal to the value or constant value of other controls.

DataTypeCheck

The data Type comparison between the value of the input control that is verified and the data Type specified by the Type attribute. Verification fails if the value cannot be converted to the specified data type.

If the Operator attribute is DataTypeCheck, The ControlToCompare and ValueToCompare attributes are ignored, and the Type attribute takes effect.

Type attribute Description

String

Specifies the string data type.

Integer

Specifies the 32-bit signed integer data type.

Double

Double-precision floating point data type.

Date

Specifies the date data type.

Currency

Specify the currency data type

3. RangeValidator: Controls for data range Verification
Main attributes:
ControlToValidate: ID of the control to be verified
ErrorMessage: the message displayed when the verified control is invalid.
MaximumValue: maximum value of the verified Control
MinimumValue: minimum value of the verified Control

 

4. RegularExpressionValidator: Data Format verification control
Main attributes:
ControlToValidate: ID of the control to be verified
ErrorMessage: the message displayed when the verified control is invalid.
ValidationExpression: a regular expression used to determine the validity. By default, the system provides several regular expressions. If you need to create a new regular expression, you must understand the significance of the regular expression.

Regular Expression Description
Square brackets "[]" Use and define acceptable characters. [Abc123] indicates that the control can only accept the six characters a, B, c, 1, 2, and 3.
Anti-collection symbol "^" Used to define unacceptable characters. [^ A-h] indicates that the control is acceptable except for 8 characters a to h.
Curly braces "{}" Defines the number of characters that must be entered. {6} indicates that only six characters can be entered; {6,} indicates that more than six characters must be entered; {} indicates that 2 to 6 characters must be entered; however, curly braces must be placed behind square brackets. For example, [a-z] {4} indicates that four characters must be entered between a and z.
Small dot "." Represents any character. For example,. {3, 6} indicates to accept 3 to 6 arbitrary characters.
Vertical Line "|" The logical symbol used to represent "or. For example, [1-9] {3, 6} | [A-Za-z] {3} indicates that 3 to 6 digits or 3 letters are allowed. (Case sensitivity can be different)
Parentheses () Used for chunking, similar to parentheses in numeric operations
Slash "\" If you want acceptable characters to contain the preceding special characters. For example, \ ([0-9] {3} \) indicates the telephone area number in the format of "(xxx )".

 

5. ValidationSummary: verification error message display control
Main attributes:
DisplayMode: Summary Display format
ShowMessageBox: whether to bring up an error message dialog box
ShowSummary: whether to display the error summary text

 

6. CustomValidator: Custom verification control
Main attributes:
ControlToValidate: ID of the control to be verified
ErrorMessage: the message displayed when the verified control is invalid.
ValidateEmptyText: whether to verify the control when the control text is empty
ClientValidationFunction: client script verification function, which is empty by default. If you want to add verification on the server, leave it blank and fill in the ServerValidate event. If you want to add verification on the client, the property value is the function name of JavaScript.
Main events:
Client verification: ClientValidationFunction = "clientv"
<Script type = "text/jscript">
Function clientv (s, e)
{
If (e. Value = "client ")
{
E. IsValid = true;
}
Else
{
E. IsValid = false;
}
}
</Script>
Server verification: ClientValidationFunction = ""
ServerValidate: Call to perform verification on the server; double-click the control or select an event.

Example: judge whether the input is an even number
1 protected void CustomValidator1_ServerValidate (object source, ServerValidateEventArgs args)
2 {
3 try
4 {
5 // If the input value is an even number, the verification is successful. Args. Value: the Value of the control to be verified.
6 if (Convert. ToInt32 (args. Value) % 2) = 0)
7 {
8 // verify
9 args. IsValid = true;
10}
11 else
12 {
13 args. IsValid = false;
14}
15}
16 catch (Exception e)
17 {
18 args. IsValid = false;
19}
20}
21
22

 

7. Disable the verification control.
Under certain conditions, you may need to avoid verification. You can disable data verification in the following three ways:

  • Disable verification in a specific control: Set the CausesValidation attribute of the control to false.
  • Disable verification control: Set the Enabled attribute of the verification control to false.
  • Disable client verification: Set the EnableClientScript attribute of the verification control to false.

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.