Today's business websites, or personal websites, there are more customers investigating. Things such as user registration will inevitably use forms. The filling of these forms is obviously controlled by the handwritten code. I admit that writing code is an interesting job, but every time you verify the form, handwriting code. We hope to improve our work efficiency. It does not spend our limited time on the verification work of the form.
Based on some other reasons, the web application is difficult to verify the user's input. The HTML 3.2 specification allows you to control the feedback of the user, but malicious or superb users can bypass it, so even if there is a browser -side handwritten code, The server also needs to be verified to ensure safety.
Below is Microsoft's staff. The commercial websites given by handwriting code control the disadvantage of the form verification of the form.
1. Although error information or icons are often adjacent to the input elements, they are almost always located in different cells of the table.
2. There is often a area in the page to summarize all errors.
3. Many sites include client script to provide faster feedback while preventing round trip between the server.
4. Many sites including client scripts display information boxes when there is an error.
5. Not only will the text input be verified, but also the drop -down list and radio button will be verified.
6. If a field is empty, the site usually displays different information or icons when it is invalid.
7. Many effective examinations can be well replaced by commonly used expressions.
8. Verification is usually based on comparison results between two inputs.
9.90% or more than 90% verification tasks are some common operations, such as checking names or postal coding. Most sites still seem to be repeating these tasks.
10. Because the difference between sites is usually too large, a perfect solution cannot be obtained to handle all the verification tasks of each site.
If you want to confirm with your colleagues that you have a very "cool" method, you can prohibit the empty value in the name segment. Remember to choose ASP.NET and look at the tutorial below.
ASP.NET processes the form verification of form verification
1. ASPX file is compiled, running
2. User input data
3. Triggering the Page_Load event
4. Change the web control attribute and prompt where there is no input
5. Export the page with HTML to the user
6. Remind users to enter again
Page_load process
Page objects include some important attributes and methods related to the server -side verification. The following table lists all the collection and metheds of the Page_load property.
Page_load method name description
ISVALID (most often used) is the most useful attribute. This attribute can check whether the entire form is effective. This check is usually performed before updating the database. Only all the objects of Validators are valid, the attribute is true, and the value is not stored in the cache.
Validators The collection of all verification objects of this page. This is a collection of objects that implement the IVALIDATOR interface.
A method called Validate in verification. The default execution method on the Page object is to turn to each verification device and require the verification device to evaluate itself.
Used to verify the verification device of the form (web control)
RequiredFieldValidator checked whether the user entered or selected any content
RegulaRXPRESSIONVALIDATOR checks the user input according to the rules. This process allows many types of examinations to be used for postal coding and phone numbers.
CompareValidator compares the input control with one fixed value or another input control. For example, it can be used in password verification fields. It can also be used to compare the dates and numbers input.
Rangevalidator is very similar to comparevalidator, but it is used to check whether the input is between two or other input control values
CustomValidator allows users to write their own code to join the verification framework.
Example description The operation of the control described above
We build a minimized .aspx page, we don't need gorgeous modification effects.
<P> Please enter the new user ID and password </p>
<FORM RUNAT = Server>
<Table>
<TR>
<TD> User ID </td>
<TD > <Input Type = Text Runat = Server ID = TXTNAME> </Td>
</Tr>
<TR>
<TD> Polar </td>
<TD> <Input Type = Password Runat = Server ID = TXTPWORD> </Td>
</Tr>
<TR>
<TD> Please re -enter the password </td>
<TD > <Input Type = Password Runat = Server ID = TXTREPWORD> </Td>
</Tr>
<TAL> <br>
<Input type = submit runat = server id = cmdsubmit value = submit>
</Form>
The program is a very harsh thing, so we must emphasize that all items are filled in, and we can do it like this. Add RequiredFieldValidator in front of each field. If the field is empty, we must display a star number (*) in front of the field and report an error next to it.
For example:
<TR>
<TD>
<Asp: RequiredField Validator Runat = Server
Controltovalidate = txtname
errorMessage = "requires user user name." > *
</Asp: RequiredFieldValidator>
</Td>
<TD> User ID: </td>
<TD > <Input Type = Text Runat = Server ID = TXTNAME> </Td>
</Tr>
Without entering any content, "*" is displayed next to the label. Error news appears. "Controltovalidate" attribute specifies the control ID required for verification. The last step is to display all errors on the top of the page.
Code:
<Asp: ValidationSummary Runat = Server Headertext = There are errors on this page:/>
Then, we need the user ID must be 6-10 characters, the password must be 4-12 letters, and must have@#$%^&*/characters.
Code: Here we have added restrictions on ID length.
<TD>
<Input type = Text Runat = Server ID = TXTNAME>
<Asp: RegularXpressionValidator Runat = Server
Controltovalidate = "TXTNAME"
ErrorMesage = "Wanlets must be composed of 6-10 letters."
ValidationXpression = "[A-Za-Z] {6,10}" />
</Td>
Then, we have to verify whether the password for two inputs is the same.
<Asp: RegularXpressionValidator Runat = Server Display = Dynamic
controltovalidate = "txtpword"
ErrorMessage = "The password must contain one of@#$%^&*/."
ValidationExpression = ".*[@#$%^&* /].*" />
<Asp: RegularXpressionValidator Runat = Server Display = Dynamic
controltovalidate = "txtpword"
ErrorMessage = "The password must be 4-12 non-blank letters."
ValidationXpression = "[ /s {4,12}" />
<Asp: CompareValidator Runat = Server
Controltovalidate = txtRepword
Controltocompare = txtpword
errorMessage = "The password does not match." />
By default, comparevalidator is compared with simple string matching. If necessary, it can make more complicated comparisons involving the date and numbers.
Several issues that need attention
About script library
Because the verification of the web control script is in the script library, the code verified by all clients is not necessary to directly send it to the page, although it seems to be done on the surface. The main script file references is similar to the following:
<Script language = "javascript" src = "/_ aspx/1.0.9999/script/webuivalidation.js"> </script>
By default, the script file will be installed in the default root directory in the "_aspx" directory, and uses a relatively rooted script Include instruction to call, which starts with a positive slope. The reference shows that each individual object does not need to include the script library, and all pages on the same computer can reference the same file. You will notice that there is also a public language version number in this path, so that different runtime versions can run on the same computer.
If you look at your default virtual root directory, you will find the file and view the content. The position of these files is specified in the config.web file. Config.web file is an XML file for most ASP+ settings. The following is the definition of the position in this file:
<Webcontrols
ClientScriptsLocation = "/_ ASPX/{0}/script/"
/>
Encourage you to read the script so that you can understand the events that occur in depth. However, it is recommended that you do not modify these scripts, because their functions are closely linked to specific runtime versions. When the version is updated, these scripts may also need to be updated accordingly. You will give up changes, or face the problem of not working. If specific projects must be changed, first backup these scripts, and then point your project to the backup file, the method is to use a private config.web file to replace the position of these files. If the string contains the format instruction "{0}", the version number will replace the instruction when running. It is best to change this position to a relative reference or absolute reference. Welcome to visit web design enthusiasts web development.