Introduction to six common validation controls in ASP. NET

Source: Internet
Author: User

  RequiredFieldValidator (field verification required) is used to check whether input values exist.

CompareValidator (comparison verification) compares two inputs according to Settings

RangeValidator (range verification) whether the input is in the specified range

RegularExpressionValidator (Regular Expression verification) Regular Expression verification control

Custom verification control

ValidationSummary (Verification summary) summarizes verification results

  The following describes how to use these controls.

  1. CompareValidator Control 

Compare the control to compare whether the input of the two controls conforms to the program settings. Do not just consider the comparison as "equal". Although equality is the most commonly used, in fact, the comparison here covers a wide range, you will understand the standard code.

The standard code for comparing controls is as follows:

<ASP: CompareValidator id = "Validator_ID" RunAt = "Server"

ControlToValidate = "Control ID to be verified"

ErrorMessage = "error message"

ControlToCompare = "Control ID to be compared"

Type = "String | Integer | Double | DateTime | Currency"

Operator = "Equal | NotEqual | GreaterThan | GreaterTanEqual | LessThan | LessThanEqual | DataTypeCheck" Display = "Static | Dymatic | None"

> Placeholder </ASP: CompareValidator>

In the above standard code:

Type indicates the Data Type of the control to be compared;

Operator indicates a comparison operation (that is, why it is not just "equal"). Here, there are 7 Comparison methods;

Other attributes are the same as RequiredFieldValidator;

Here, pay attention to the difference between ControlToValidate and ControlToCompare. If operate is GreateThan, ControlToCompare must be greater than ControlToValidate. Now, we should understand the meaning of the two? For the example program, refer to the RequiredFieldValidator control and design it according to the standard code.

  Ii. Use of RequiredFieldValidator (field verification is required)

  The standard code used by the RequiredFieldValidator control is as follows:

<ASP: RequiredFieldValidator id = "Validator_Name" Runat = "Server" ControlToValidate = "Control name to be checked" ErrorMessage = "error message" Display = "Static | Dymatic | None">

Placeholder

</ASP: RequiredFieldValidator>

In the above standard code:

ControlToValidate: indicates the ID of the control to be checked;

ErrorMessage: indicates the error message when the check is invalid;

Display: Display Mode of the error message. Static indicates that the error message of the control occupies a certain position on the page. Dymatic indicates that the page control is occupied only when the control error message appears; none indicates that the error is not displayed when it appears, but it can be displayed in ValidatorSummary;

Placeholder: indicates that when the Display is Static, the error information occupies the page space as large as the "Placeholder;

Now let's look at an instance:

<ASP: TextBox id = "txtName" RunAt = "Server"/>

<ASP: RequiredFieldValidator id = "Validator1" Runat = "Server" ControlToValidate = "txtName" ErrorMessage = "name must be entered" Display = "Static" & gt;

* Name is required

</ASP: RequiredFieldValidator>

In the preceding example, check whether the txtName control has been input. If not, an error message "name is required" is displayed ". Is it easy?

Note: The code above and the code of other controls below should be put into Form. Unlike ASP, it is best to write Form as follows:

<Form RunAt = "Server">

Other code

</Form> in this way, the submission takes effect only when the Form is executed on the server;

  3. RangeValidator Control 

Verify that the input is in a certain range and the range is determined by MaximumValue (maximum) and MinimunVlaue. The standard code is as follows:

<ASP: rangeValidator id = "Vaidator_ID" Runat = "Server" controlToValidate = "Control ID to be verified" type = "Integer" MinimumValue = "minimum" MaximumValue = "maximum" errorMessage = "error message ""Display =" Static | Dymatic | None ">

Placeholder

</ASP: RangeValidator>

In the above Code:

Use MinimumValue and MaximumValue to define the input value range of the control, and use type to define the type of the input value of the control.

  Iv. RegularExpresionValidator (Regular Expression) Control 

Regular Expression verification controls are very powerful. You can easily construct verification methods by yourself. Let's take a look at the Standard Code:

<ASP: RegularExpressionValidator id = "Validator_ID" RunAt = "Server" ControlToValidate = "Control name to verify" ValidationExpression = "Regular Expression" errorMessage = "error message" display = "Static">

Placeholder

</ASP: RegularExpressionValidator>

In the above standard code, ValidationExpression is the focus. Let's take a look at its structure:

In ValidationExpression, different characters indicate different meanings:

"." Indicates any character;

"*" Indicates that it is easy to combine with other expressions;

"[A-Z]" represents any capital letter;

"D" indicates a number;

Note that quotation marks are not included in the preceding expressions;

Example:

Regular Expression: ". * [A-Z]" indicates a combination of any character starting with a number followed by an uppercase letter.

  5. ValidationSummary Control 

This control collects all verification error messages on this page and organizes them to be displayed later. The standard code is as follows:

<ASP: ValidationSummary id = "Validator_ID" RunAT = "Server" HeaderText = "header information" ShowSummary = "True | False" DiaplayMode = "List | BulletList | SingleParagraph">

</ASP: ValidationSummary>

In the above standard code, HeadText is equivalent to the HeadText of the table. DisplayMode indicates that the error message is displayed in the following way: List is equivalent to <BR> in HTML; BulletList is equivalent to <LI> in HTML; singleParegraph indicates that error messages are not separated;

  6. mvalidator Control 

The control defines the verification method using a custom function. The standard code is as follows:

<ASP: customValidator id = "Validator_ID" RunAt = "Server" controlToValidate = "control to be verified" onServerValidateFunction = "verification function" errorMessage = "error message" Display = "Static | Dymatic | None"> placeholder </ASP: customValidator>

In the above Code, you must define a function to verify the input.

Related Article

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.