C # verify the control

Source: Internet
Author: User

C # verify the usage of the controlASP. 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.

HoweverIf 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. NET, you can not only easily verify user input, but also choose whether to verify on the server or on the client, so you don't have to worry about it any more,ProgramMembers can focus on the design of the main program.
ASP. NET publicSat.Verification controls are as follows:

Control name Function Description

Requiredfieldvalidator (field verification is required)

Used to check whether the input value is comparevalidator (comparison verification)

Compare two input rangevalidator by settings (range verification)

Whether the input is in the specified range of regularexpressionvalidator (Regular Expression verification)

Regular Expression verification control customvalidator (custom verification)

Custom verification control validationsummary (Verification summary)

Summary of verification results

The following describes how to use these controls:

I. standards used by the requiredfieldvalidator control for the use of requiredfieldvalidator (field verification is required)Code<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: displays the error message; static indicates that the control's error information occupies a certain position on the page; dymatic indicates that the control's error information only occupies the page control when it appears; none indicates that the error is not displayed, but can be displayed in validatorsummary; placeholder: indicates that when the display is static, the error message occupies a page as large as the "Placeholder ". Area space. 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 required </ASP: requiredfieldvalidator> in the preceding example, check whether the txtname control has been input. If not, the system displays the error message "name required ". 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;

2. The comparevalidator (comparison verification) control compares the input of the two controls to check whether they comply with the program settings ", although equality is the most widely used, the comparison here involves a wide range, and you will understand the standard code. The standard code for comparing controls is as follows: placeholder in the above standard code: type indicates the Data Type of the control to be compared; operator indicates a comparison operation (that is, why is it more than just "phase And so on), here, there are seven ways to compare; other attributes are the same as requiredfieldvalidator; here, pay attention to the difference between controltovalidate and controltocompare. If operate is greatethan, then, it must be that controltocompare is more legal than controltovalidate. Now, should you understand the meaning of both of them? For the example program, refer to the requiredfieldvalidator control and design it according to the standard code.

3. The rangevalidator control verifies that the input is within a certain range. The range is determined by maximumvalue (max) 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 control input value range, and use type to define the type of the control input value.

4. regularexpresionvalidator (Regular Expression) controls 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. Now let's take a look at its structure: In validationexpression, different characters indicate different meanings :". "represents any character;" * "represents an easy combination with other expressions;" [A-Z] "represents any uppercase letter ; "\ D" indicates a number. Note that quotation marks are not included in the above expression. For 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 for display 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 the display method of error information: List is equivalent to HTML; bulletlist is equivalent to <li> in HTML; singleparegraph indicates that error messages are not separated;

6. customvalidator (custom verification) control this control uses a custom function definition verification method. 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. 2. Verify the usage of the control: (1) set the property of the Focus setfocusonerror to true. (2) When we use a text box to input a lot of data, we need to change the textmode attribute of the figure textbox to multiline for multiple rows. In this case, the maxlength attribute cannot work, during verification, we need to use the verification control regularexpressionvalidator, where the regular expression ['<>'] {0, n} where N is the maximum value set.

VII. Summary ASP. NET verification controls have been introduced. You can find that they are very powerful and don't worry about verification any more. Of course, the above introduction is not particularly detailed. Please read the Microsoft SDK yourself.

Reprinted: http://www.cnblogs.com/yuanzhengang/archive/2008/01/18/1043964.html

 

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.