ASP. NET is the next-generation Web development tool launched by Microsoft. Its powerful functions immediately attract a large number of Web developers to invest in it. Now, let's take a look at the ASP. NET verification control, feel the powerful functions of ASP. NET, and facilitate our current web development.
Web developers, especially ASP developers, have always been annoyed with data verification. When you are not easy to write the subject of the data submission program, you have to spend a lot of time verifying that each user input is legal. If you are familiar with JavaScript or VBScript, you can use these scripting languages for easy verification, but you need to consider whether your browser supports these scripting languages. If you are not familiar with these languages or want to support all your browsers, it must be verified in the ASP program, but such verification will increase the burden on the server. Now, with ASP., programmers can focus on the design of the main program.
ASP. NET has six public verification controls:
Widget 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
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
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.
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. 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.
VII. Summary
ASP. NET verification controls have been introduced. You can find that they are powerful and do not worry about verification any more. Of course, the above introduction is not particularly detailed. Please read the Microsoft SDK yourself.
ASP. NET is the next-generation Web development tool launched by Microsoft. Its powerful functions immediately attract a large number of Web developers to invest in it. Now, let's take a look at the ASP. NET verification control, feel the powerful functions of ASP. NET, and facilitate our current web development.
Web developers, especially ASP developers, have always been annoyed with data verification. When you are not easy to write the subject of the data submission program, you have to spend a lot of time verifying that each user input is legal. If you are familiar with JavaScript or VBScript, you can use these scripting languages for easy verification, but you need to consider whether your browser supports these scripting languages. If you are not familiar with these languages or want to support all your browsers, it must be verified in the ASP program, but such verification will increase the burden on the server. Now, with ASP., programmers can focus on the design of the main program.
ASP. NET has six public verification controls:
Widget 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
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
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.
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. 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.
VII. Summary
ASP. NET verification controls have been introduced. You can find that they are powerful and do not worry about verification any more. Of course, the above introduction is not particularly detailed. Please read the Microsoft SDK yourself.