Use of six common authentication controls in ASP. NET
One of the powerful functions of ASP. NET is its rich Web controls. Here we will talk about one of them-verification controls.
. It summarizes various web data verification methods and is designed as a general ASP. NET control today.
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
--------------------------------------------------------------------------------
Directory
1. verification control Overview
2. Verify the usage of the control
--------------------------------------------------------------------------------
1. verification control Overview
As the name suggests, a verification control is a control that verifies the correctness of user input data. For example, after a user inputs data in a text box,
A prompt message is displayed, indicating that the verified data is invalid. The verification process can be executed on the server or
It can be executed on the client. The verification code run on the client is executed before the data is submitted.
Sequential performance. The validation control of ASP. NET can perform multiple verifications, such as data range check, comparison between data and custom
Verification, etc. The following content will introduce these verification controls one by one.
--------------------------------------------------------------------------------
2. Verify the usage of the control
① Requiredfieldvalidator Control
When the control on the page requires that data be input, requiredfieldvalidator takes effect,
The controltovalidate attribute selects the control to be verified, while the errormessage attribute is displayed after the verification is invalid.
Error message.
1 <HTML> 2 Runat = "server" 10 errormessage = "Please enter your name" 11 controltovalidate = "textbox1"> </ASP: requiredfieldvalidator> 12 <asp: button id = "button1" runat = "server" text = "button"> </ASP: button> 13 </form> 14 </body> 15 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 error information; static indicates that the error information 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 page control is not displayed when the error occurs, but you can
Shown in validatorsummary;
Placeholder: indicates that when the display is static, the error information occupies the page space as large as the "Placeholder ".
② Comparevalidator Control
The comparevalidator control is used to compare data consistency between two input controls. It can also be used to verify
The data type of the content in the control, such as integer and string. Controltocompare and controltovalidate attributes
Two controls used for comparison.
1 <HTML> 2 Id = "txtcfm" runat = "server" 12 textmode = "password"> </ASP: textbox> </P> 13 <p> 14 <asp: button id = "button2" runat = "server" text = "button"> </ASP: button> 15 <asp: comparevalidator id = "comparevalidator1" runat = "server" 16 errormessage = "Password error! "17
Controltovalidate = "txtcfm" 18 controltocompare = "txtpwd"> </ASP: comparevalidator> </P> 19 </form> 20 </body> 21 Compare the control to compare whether the input of the two controls conforms to the program settings.
Equality is the most widely used. In fact, the comparison here involves a wide range. You will understand the standard code.
The standard code for comparing controls is as follows:
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 "equal"). Here,
There are 7 Comparison methods;
Other attributes are the same as requiredfieldvalidator;
Note the difference between controltovalidate and controltocompare. If operate is
Greatethan, it is legal that controltocompare must be greater than controltovalidate.
Understand the meaning of the two, right? For more information about the sample code, see the requiredfieldvalidator control.
Designed.
③ Rangevalidator Control
The rangevalidator control can be used to determine whether the user input value is within a specific range.
And minimumvalue are used to set the maximum and minimum values of the range.
1 <HTML> 2 Controltovalidate = "txtage" 11 errormessage = "age error! "Maximumvalue =" 99 "12 minimumvalue =" 1 "> </ASP: rangevalidator> </P> 13 </form> 14 </body> 15 Use minimumvalue and maximumvalue to define the control input value range, and use type to define the type of the control input value
.
④ Regularexpressionvalidator Control
The regularexpressionvalidator control can be used to determine whether the expression entered by the user is correct, such as the phone number or zip code.
, URL, etc. Select the control to be verified for the controltovalidate attribute, and write the validationexpression attribute
The style of the expression to be verified. The following example shows the code for verifying the zip code.
1 <HTML> 2 Id = "regularexpressionvalidator1" runat = "server" 10 controltovalidate = "txtpostalcode" errormessage = "Postal code error! "11 validationexpression =" D6} "> </ASP: regularexpressionvalidator> </P> 12 </form> 13 </body> 14 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]" represents a combination of any character starting with a number followed by an uppercase letter
⑤ Customvalidator Control
The customvalidator control is used to perform user-defined verification, which can be either a server or a customer
The following code is an example of using the client to verify the zip code.
1 <HTML> 2 Runat = "server" 10 controltovalidate = "textbox1" 11 errormessage = "customvalidator" 12 clientvalidationfunction = "clientvalidate"> 13 </ASP: customvalidator> </P> 14 </form> 15 </body> 16 Arguments) 19 if isnumeric (arguments. value) and Len (arguments. Value) = 6 then20 arguments. isvalid = true21 else22 arguments. isvalid = false23 end if24 end sub25 </SCRIPT>
⑥ Validationsummary Control
This control outputs all verification errors on the page as a list. The display mode of the List is set by the displaymode attribute.
.
1 <HTML> 2 Runat = "server" 10 controltovalidate = "txtage" errormessage = "age error! "11 DISPLAY =" NONE "> </ASP: requiredfieldvalidator> <br> 12 Postal code: 13 <asp: textbox id = "txtpostalcode" runat = "server"> </ASP: textbox> 14 <asp: requiredfieldvalidator id = "requiredfieldvalidator2"
Runat = "server" 15 controltovalidate = "txtpostalcode" errormessage = "Postal code error! "16 display =" NONE "> </ASP: requiredfieldvalidator> </P> 17 <p> 18 <asp: button id = "button1" runat = "server" text = "button"> </ASP: button> </P> 19 <p> 20 <asp: validationsummary
Id = "validationsummary1" runat = "server" 21 headertext = "you must enter a value in the following22 fields:"> </ASP: validationsummary> </P> 23 </form> 24 </body> 25