Check the asp+ form to see if the fill is correct

Source: Internet
Author: User
Tags comparison config empty net string version valid versions
Asp+ today's commercial web sites, or personal sites, how many have customer survey Ah, user registration and other things, will inevitably use the form, these forms are filled out correctly or not, obviously by the way of handwritten code to control. I admit that writing code is an interesting job, but every time we validate the form to write code, we want to improve our productivity, not to spend our limited time on the validation of our forms.
For other reasons, Web applications are hard to verify user input, and the HTML 3.2 specification allows you to control user feedback, but malicious or highly skilled users can bypass it, so even with the browser-side handwriting code, the server side also needs to be validated to ensure security.
Here is Microsoft's staff, given the drawbacks of the commercial web site using handwritten code to control form validation.
1. Although error messages or icons are often adjacent to input elements, they are almost always in different cells of the table.
2. There is often an area on the page that summarizes all errors.
3. Many sites contain client-side scripting to provide faster feedback while preventing the free round-trip between servers.
4. Many sites that contain client script display information boxes when errors occur.
5. Not only validates text input, but also verifies Drop-down lists and radio buttons.
6. If a field is empty, the site usually displays information or icons that are different when the entry is not valid.
7. Many validation checks can be a good substitute for commonly used expressions.
8. Validation is usually based on the comparison between two inputs.
9. More than 90% or 90% of the validation tasks are common operations, such as checking names or postal codes. Most sites still seem to be doing this repeatedly.
10. Because the differences between sites are usually too large to get a perfect solution to handle all the validation tasks for each site.
If you want to prove to your colleagues that you have a "cool" method, you can disable the entry of NULL values in the Name field, remember to choose ASP.net, and look at the following tutorial.
asp.net process for form validation
1. aspx files are compiled and run
2. User input data
3. Triggering Page_Load events
4. Change Web control properties to indicate where there is no input
5. Re-export the page to the user in HTML
6. Remind users to enter again
Page_Load process
The Page object contains some important properties and methods related to server-side validation. The following table lists all the collect and methed for the Page_Load property.
Page_Load Method Name Description
IsValid (most frequently used) this is the most useful attribute. This property can check whether the entire form is valid. This check is typically done before the database is updated. This property is true only if all objects in the validators set are valid, and the value is not cached.
Validators the collection of all validation objects for this page. This is a collection of objects that implement the IValidator interface.
Validate a method that is invoked at validation time. The default way to perform on the Page object is to go to each validator and ask the validators to evaluate themselves.
Validator used to validate the form (Web control)
RequiredFieldValidator Check whether the user has entered or selected any Content
RegularExpressionValidator checks user input according to regular expressions. This process allows for many kinds of checks that can be used in postal codes and telephone numbers.
CompareValidator compares an input control with a fixed value or another input control. For example, it can be used in a password validation field. You can also use to compare the dates and numbers you enter.
RangeValidator is very similar to CompareValidator, except that it is used to check whether the input is between two values or other input controls
CustomValidator allows users to write their own code to join the validation framework.
Example illustrates the operation of the control described above
We build a minimized. aspx page, and we don't need gorgeous cosmetic effects.
<p> Please enter a 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> Password </td>
<td> <input Type=password runat=server id=txtpword> </td>
</tr>
<tr>
<td> Please re-enter password </td>
<td> <input Type=password runat=server id=txtrepword> </td>
</tr>
<table> <br>
<input type=submit runat=server id=cmdsubmit value=submit>
</form>

The program is a very demanding thing, so, to emphasize all the items are filled out, we can do so. Add RequiredFieldValidator before each field. If the input field is empty, we want to display an asterisk (*) in front of the field and report an error next to it.
For example:
<tr>
<td>
<asp:requiredfieldvalidator Runat=server
Controltovalidate=txtname
Errormessage= "requires user username. >
</asp:RequiredFieldValidator>
</td>
<td> User ID: </td>
<td> <input type=text runat=server id=txtname> </td>
</tr>
No content is entered, then "*" is displayed next to the label. An error message appears. The "ControlToValidate" property specifies the ID of the control that needs to be validated. The final step is to show all the errors at the top of the page.
Code:
<asp:validationsummary runat=server headertext= This page has an error:/>

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 the length of IDs.
<td>
<input Type=text Runat=server id=txtname>
<asp:regularexpressionvalidator Runat=server
Controltovalidate= "Txtname"
Errormesage= "Password must consist of 6-10 letters. "
Validationexpression= "[a-za-z]{6,10}"/>
</td>
Next, we want to verify that the password entered two times is the same.
<asp:regularexpressionvalidator Runat=server display=dynamic
Controltovalidate= "Txtpword"
Errormessage= "Password must contain one of the @#$%^&*/. "
Validationexpression= ". *[@#$%^&*/].*"/>
<asp:regularexpressionvalidator Runat=server display=dynamic
Controltovalidate= "Txtpword"
errormessage= "Password must be 4-12 non-blank letters. "
Validationexpression= "[\s{4,12}"/>
<asp:comparevalidator Runat=server
Controltovalidate=txtrepword
Controltocompare=txtpword
errormessage= "Password does not match. "/>
By default, CompareValidator only makes a simple string matching comparison. If necessary, it can carry out more complex comparisons involving dates and numbers.
A few questions to pay attention to
About the Script library
Because validating Web control scripts are in the script library, you do not have to send all client-validated code directly to the page, although that appears to be the case. The main scripting file references are similar to the following:
<script language= "javascript" src= "/_aspx/1.0.9999/script/webuivalidation.js" > </script>
By default, the script file is installed in the default root directory in the "_aspx" directory and is invoked with a script include directive relative to the root, which begins with a forward slash. This reference indicates that each individual object does not have to contain a script library, and that all pages on the same computer can reference the same file. You will notice that there is also a common language runtime version number in the path so that different runtime versions can run on the same machine.
If you look at your default virtual root directory, you will find the file and view its contents. The location of these files is specified in the Config.web file. The Config.web file is an XML file that is used for most asp+ settings. The following is the definition of the location in the file:
<webcontrols
clientscriptslocation= "/_aspx/{0}/script/"
/>
You are encouraged to read the script to get an insight into the events that occurred. However, it is recommended that you do not modify these scripts because their functionality is tightly linked to a specific runtime version. When versions are updated at run time, the scripts may also require appropriate updates, and you will either discard the changes or face problems with the script not working. If a specific project must change these scripts, back up the scripts, and then point your project to the backup file by using a private Config.web file instead of the location of the files. If the string contains the format directive "{0}", the runtime version number replaces the directive. It is a good idea to change the location to a relative or absolute reference.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.