[Repost] ASP.NET Lecture (3) - Enter the verification control

xiaoxiao2021-03-06  24

3.1 ASP.NET solves the problem of ASP: verifying the correctness of formula

Today's business website, or personal website, how many customers have a customer investigation, something such as user registration, will inevitably use the form, the correct or not, which is obviously controlled by the handwritten code. I admit that writing code is an interesting job. However, every verification form comes to handwritten code, we want to improve our work efficiency, not to spend our very limited time to spend the verification work of the form.

Based on some reasons, the web application is very bad to verify the user's input. HTML 3.2 specification allows you to control users' feedback, but malicious or technically superb users can bypass, so even the browser-ended handwritten code, The server side also needs to be verified to ensure security.

Here is Microsoft staff, giving the business website with handwritten code control form validation.

1. Although error messages or icons are often adjacent to the input elements, they are almost always located in different cells.

2. There is often a region in the page to summarize all errors.

3. Many sites contain client scripts to provide faster feedback while preventing rounds with the server in white.

4. Many sites that contain client scripts will display the information box when an error occurs.

5. Not only will the text input, but also verify the drop-down list and the radio button.

6. If a field is empty, the site usually displays different information or icons when the entry is invalid.

7. Many effectiveness checks can replace common expressions well.

8. Verification is usually based on two input comparisons.

9. 90% or more verification tasks is some common operations, such as check names or postal codes. Most sites seem to still be repeated.

10. Because the difference between the site is usually too big, a perfect solution cannot be obtained to handle all verification tasks for each site.

If you want to confirm that you have a very "cool" method, you can ban you enter null values ​​in the name field, remember to choose ASP.NET, and look at the tutorial below.

3.2 ASP.NET for formal verification process

1. ASPX file is compiled, run

2. User input data

3. Trigger Page_Load Event

4. Change the Web Control Properties, no input

5. Return the page with HTML to the user

6. Remind the user again

3.3 Page_Load Process

Page objects contain some important properties and methods related to server-side verification. The following table lists all Collect and Methed of the Page_Load property.

Page_Load method name Description Isvalid (most often) This is the most useful attribute. This property can check if the entire form is valid. This check is usually performed before updating the database. Only all objects in the Validators set are all valid, this property is true and does not store this value into the cache. Validators This page All validation objects are set. This is a collection of objects that implement the IVALIDATOR interface. Validate is called when verifying. The default execution method on the PAGE object is to each validator and require each verifier to evaluate it.

3.4 Verifier (web control) used to verify the form

RequiredFieldValidator Checks the user if the user enters or selects the regular expression to check the user input according to the rule expression. This process allows many types of inspections that can be used for postal coding and telephone numbers. CompareValidator compares 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 date and numbers of the input. RangeValidator is very similar to CompareValidator, just it is used to check if the input is between two values ​​or other input controls. CustomValidator allows users to write their own code to join in the verification framework. 3.5 Example Description The operation of the control described above

We build a minimized .aspx page, we don't need gorgeous modifications.

Please enter a new user ID and password

User ID
password
please Re-enter the password

The program is a very harsh thing, so it is necessary to emphasize all items, we can do it. Add RequiredFieldValidator before each field. If the input field is empty, we have to display an asterisk (*) before the field and report an error next to it.

E.g:

* User ID: If no content is input, "*" is displayed next to the tag. An error message appears. The "ControlTovAlidate" property specifies the control ID that needs to be verified. The final step is to display all the error phenomenon to the top of the page.

Code:

Then, we need the user ID must be 6-10 characters, the password must be 4-12 letters, and there must be @ # $% ^ & * / characters.

Code: Here we increase the limitations of ID lengths.

Then, we have to verify that the passwords input twice are the same.

By default, CompareValidator only makes a simple string matching comparison. If desired, it can make more complex comparisons involving dates and numbers. 3.6 Several questions that need attention

About script library

Because the Web Control Script is in the script library, you don't have to send all the client authenticated code directly to the page, although it seems to be done on the surface. The main script file is quoted like this: