ASP. NET verification control and ASP. NET verification control
To verify whether the text box is empty, whether the user exists, and whether the input is legal, we have to write code separately when working on the data center charging system and learn ASP. NET verification controls, saving a lot of trouble.
ASP. NET allows you to easily verify user input. Let's take a look at the six authentication controls public in ASP. NET.
1. Basic verification control:
(1) RequiredFieldValidator:Restrict empty fields(Field verification is required) used to check whether input values exist.
For example, when logging on, You must verify that the entered user name and password are empty.
<Asp: RequiredFieldValidator ID = "RequiredFieldValidator1" runat = "server" ControlToValidate = "txtUserName" ErrorMessage = "username required"> </asp: RequiredFieldValidator>
(2) Basic comparison Control
A. CompareValidator:Compare two fieldsComparison verification) compare two input values according to Settings
Example: When we change the password, you can use this control to check whether the password entered for the first time is the same as the password entered for the second time.
<Asp: CompareValidator ID = "inherit" runat = "server" ControlToCompare = "txtPassword1" ControlToValidate = "txtPassword2" ErrorMessage = "two passwords inconsistent"> </asp: CompareValidator>
B. RangeValidator:Check the specified range(Range verification) whether the input is in the specified range
For example, the range of input information
<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 "> </ASP: rangeValidator>
For example, the range of input information
(3) RegularExpressionValidator:Use a regular expression to check the value(Regular Expression verification) Regular Expression verification control
<ASP: regularExpressionValidator id = "Validator_ID" RunAt = "Server" ControlToValidate = "Control name to be verified" ValidationExpression = "Regular Expression" errorMessage = "error message" display = "Static"> </ASP: regularExpressionValidator>
(4) CustomValidator:Check the value through the client or server function(Custom verification) custom verification control
<ASP: customValidator id = "Validator_ID" RunAt = "Server" controlToValidate = "control to be verified" onServerValidateFunction = "verification function" errorMessage = "error message" Display = "Static | Dymatic | None"> </ASP: customValidator>
3. Verify the summary Control
(1) ValidationSummary:Listing verification errors for all controls on the page(Verification summary) Summary of verification results
Example: summary of all verification information
<ASP: ValidationSummary id = "ValSum" RunAT = "Server" HeaderText = "so the error is:" DiaplayMode = "SingleParagraph"> </ASP: validationSummary> <span style = "font-family: KaiTi_GB2312; background-color: rgb (255,255,255);"> </span>
Page. IsValid attributes:
<Scrip language = "C #" runat = "server"> Void subbtn (Object Src, EventArge E) {if (Page. isValid = true) <span style = "white-space: pre"> </span> {lblMessage. text = "Page valid" ;}}</script>
Page: The current whole Page. If any one fails to pass verification, the value is false.
Summary:
Each verification control performs a specific type of verification and displays a custom message prompt when the verification fails. The verification control provides great help for our development. We can verify various verifications without having to spend too much code.