C # verify the usage of the control

Source: Internet
Author: User

C # verify the usage of the control
ASP. NET provides developers with a complete set of server controls to verify whether user input information is valid. These controls are as follows:
      1. requiredfieldvalidator: Verify a required field. If this field is not entered, information cannot be submitted.

      2. comparevalidator: Comparison verification. Compare the values of two fields, such as passwords and passwords, and compare a field with a specific value.

      3. rangevalidator: range verification. Verify whether a field is in a certain range. For example, if the score field is 0 ~ In the range of 100.

      4. regularexpressionvalidatOr: Regular Expression verification. It verifies the format of user input fields based on regular expressions, such as email, ID card, and phone number.

      5. customvalidator: You can use this control when running the customized client JavaScript or VBScript function.

      So how do I use the verification control?

      1. Run the command aspnet_regiis-C to copy the ASP. NET client script file.

 

      2. Place the verification control on the right of the control to be verified.

 

      3. Modify the controltovalidate attribute of the verification control to the name of the control to be verified.

      4. Modify the errormessage attribute of the verification control to a more specific error description, such as "enter the correct email password", "inconsistent passwords", and "required fields.

      5. If you are using requiredfieldvalidator, the verification control is required. This step is OK. However, if you are using several other controls, you need to make the following settings:

      A. The comparevalidator control compares the values of the two controls. You must set the controltocompare, operator, and type attributes. To compare a control with a specific value, you must modify the valuetocompare, operator, and type attributes.

      B. The rangevalidator control sets the minimum and maximum values in the minimunvalue and maximumvalue attributes respectively. You also need to change the type attribute to currency or integer.

      C. regularexpress control: Use the validationexpress attribute to select a new regular expression from the list.

      Finally, you must note that each field can be verified using multiple weekly verification controls.

Now ASP., programmers can focus on the design of the main program.

ASP. NET has six public verification controls:

Control name           Function Description

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. 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 = "Name of the control 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 required"

Display = "static">

* 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 form is executed on the server, and the submission will be valid;

2. 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.

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 be verified"

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, and displaymode indicates the display mode of error information: List is equivalent
; 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.