Asp.net 6 verification controls

Source: Internet
Author: User

Form validators (Web controls)

Requiredfieldvalidator Check whether the user has entered or selected any content
Regularexpressionvalidator Check user input based on the Rule expression. This process allows you to perform many types of checks, such as ZIP code and phone numbers.
Comparevalidator Compare the input control with a fixed value or another input control. For example, it can be used in the password verification field. It can also be used to compare the input date and number.
Rangevalidator Similar to comparevalidator, it is used to check whether the input is between two values or other input control values.
Customvalidator Allow users to write their ownCodeTo join the verification framework.


ASP. NET is the next-generation Web development tool launched by Microsoft. Let's take a look at the validation controls of ASP. NET and feel the powerful functions of ASP. NET. With ASP. NET, you can not only easily verify user input, but also choose whether to verify on the server or on the client. You don't have to worry about it any more,ProgramMembers can focus on the design of the main program.

ASP. NET has six public verification controls:

Widget name and function description
Used to check whether the input value is requiredfieldvalidator (required for field verification)
Compare two input values by settings: comparevalidator (comparison verification)
Whether the input is in the specified range: rangevalidator (range verification)
Regular Expression verification control: regularexpressionvalidator (Regular Expression verification)
Custom verification control: customvalidator (custom verification)
Conclusion Verification Result: validationsummary (Verification summary)

Next, let's take a look at the use of these controls
I. 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.

4: regularexpresionvalidator (Regular Expression) Control
the regular expression verification control is very powerful. You can easily construct verification methods by yourself. Let's take a look at the Standard Code:
controltovalidate = "Control name to be verified"
validationexpression = "Regular Expression"
errormessage = "error message"
display = "static"> placeholder

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.

V. 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 the error message:
List is equivalent to <br>;
Bulletlist is equivalent to <li>;
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:

controltovalidate = "control to be verified"
onservervalidatefunction = "verification function"
errormessage = "error message"
display = "static | dymatic | none"> placeholder
in the preceding 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.