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. NETThe six public verification controls are as follows:
Widget name Function Description
Requiredfieldvalidator(Field verification is required) 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
Customvalidator(Custom verification) 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)
RequiredfieldvalidatorControlsCodeAs 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. 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.