Asp. NET Lectures (3)-Input validation controls

Source: Internet
Author: User
Tags comparison config empty reference relative valid versions root directory
Asp.net| Control

3.1 asp.net solves the problem of ASP: Verify the correctness of form filling

Today's commercial websites, 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.

3.2 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

3.3 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.


3.4 Validator (Web control) used to validate the form
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 the values of other input controls.
CustomValidator allows users to write their own code to join the validation framework.


3.5 Examples illustrate the operation of the control described above

We build a minimized. aspx page, and we don't need gorgeous cosmetic effects.

Please enter a new user ID and password
















User ID
Password
Please re-enter password






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:



Controltovalidate=txtname
Errormessage= "requires user username. >


User ID:


If no content is entered, the "*" 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:



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.



Controltovalidate= "Txtname"
Errormesage= "Password must consist of 6-10 letters. "
Validationexpression= "[a-za-z]{6,10}"/>

Next, we want to verify that the password entered two times is the same.

Controltovalidate= "Txtpword"
Errormessage= "Password must contain one of the @#$%^&*/. "
Validationexpression= ". *[@#$%^&*/].*"/>
Controltovalidate= "Txtpword"
errormessage= "Password must be 4-12 non-blank letters. "
Validationexpression= "[\s{4,12}"/>
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.

3.6 Several issues to be aware of

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:

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:

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.
3.7 A complete case

We're going to do a magazine subscription system now, to user input email, enter two times password, password to be the same, password requirements security, so must include (!@#$%^&*+;:), enter the user's phone, the longest 20, must choose credit Card, input card number, card number 15, the program is as follows, So easy, everybody look.


<%
Public Sub OnSubmit (source as Object, E as EventArgs)
If Page.IsValid Then
' Data warehousing and other operations
End If
End Sub
%>
Subscribe to fashion Life magazine




Headertext= "You must complete all the options"
Displaymode= "Singleparagraph"
Font-name= "Verdana"
Font-size= "12"
/>

!--sign-in-->
<table border=0 width=600>
<tr> <TD colspan=3>
<table border=0 cellpadding=0 cellspacing=0 width= "100%"
<tr> <td>
<font face=geneva,arial size=-1> <b> sign-in information </b>
</font>
</td> </tr>
</table>
</td> </tr>
<tr>
<TD align=right>
<font face=arial size=2> Enter your email </font>
</td>
<td>
<asp:textbox id=email width=200px maxlength=60 runat=server/>
</td>
<td>
<asp:requiredfieldvalidator id= "Emailreqval"
controltovalidate= "Email"
Errormessage= "your email. "
Display= "Dynamic"
Font-name= "Verdana" font-size= "12"
runat=server>
*
</asp:RequiredFieldValidator>
<asp:regularexpressionvalidator id= "Emailregexval"
controltovalidate= "Email"
Display= "Static"
validationexpression=
"^[\w-]+@[\w-]+\. (com|net|org|edu|mil) $ "
Font-name= "Arial" font-size= "11"
runat=server>
Not a correct email please enter a correct email
</asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<TD align=right>
<font face=arial size=2> Password: </font>
</td>
<td>
<asp:textbox id=passwd textmode= "password" maxlength=20
runat=server/>
</td>
<td>
<asp:requiredfieldvalidator id= "Passwdreqval"
Controltovalidate= "passwd"
errormessage= "password. "
Display= "Dynamic"
Font-name= "Verdana" font-size= "12"
runat=server>
*
</asp:RequiredFieldValidator>
<asp:regularexpressionvalidator id= "Passwdregexbal"
Controltovalidate= "passwd"
Validationexpression= ". *[!@#$%^&*+;:].*"
Display= "Static"
Font-name= "Arial" font-size= "11"
Width= "100%" runat=server>
The password must include one of the following (!@#$%^&*+;:)
</asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<TD align=right>
<font face=arial size=2> Please enter another password again: </font>
</td>
<td>
<asp:textbox id=passwd2 textmode= "password" maxlength=20
runat=server/>
</td>
<td>
<asp:requiredfieldvalidator id= "Passwd2reqval"
Controltovalidate= "Passwd2"
Errormessage= Enter the password again. "
Display= "Dynamic"
Font-name= "Verdana" font-size= "12"
runat=server>
*
</asp:RequiredFieldValidator>
<asp:comparevalidator id= "CompareValidator1"
Controltovalidate= "Passwd2" controltocompare= "passwd"
Display= "Static"
Font-name= "Arial" font-size= "11"
runat=server>
Two times the password entered how is not the same:
</asp:CompareValidator>
</td>
</tr>
<tr> <td colspan=3> </td> </tr>
The!--personalization Information-->
<tr> <TD colspan=3>
<table border=0 cellpadding=0 cellspacing=0 width= "100%"
<tr> <td> <font face=geneva,arial size=-1>
<b> Personal Information </b> </font>
</td> </tr>
</table>
</td> </tr>
<tr>
<TD align=right>
<font face=arial size=2> Tel: </font>
</td>
<td>
<asp:textbox id= "Phone" maxlength=20 runat= "Server"/>
</td>
<td>
<asp:requiredfieldvalidator id= "Phonereqval"
Controltovalidate= "Phone"
Errormessage= "Phone. "
Display= "Dynamic"
Font-name= "Verdana" font-size= "12"
runat=server>
*
</asp:RequiredFieldValidator>
</td>
</tr>
<tr> <td colspan=3> </td> </tr>

!--Credit card Info-->
<tr>
<TD colspan=3>
<font face=arial size=2> <b> Credit card information </b> </font>
</td>
</tr>
<tr>
<TD align=right>
<font face=arial size=2>card Type: </font>
</td>
<td>
<asp:radiobuttonlist Id=cctype
Font-name= "Arial" repeatlayout= "Flow" runat=server>
<asp:ListItem> China Merchants Bank card </asp:ListItem>
<asp:ListItem> Construction Bank Dragon Card </asp:ListItem>
</ASP:RadioButtonList>
</td>
<td>
<asp:requiredfieldvalidator id= "Cctypereqval"
Controltovalidate= "Cctype"
errormessage= "credit card type. "
Display= "Static"
Initialvalue= ""
Font-name= "Verdana" font-size= "12"
runat=server>
*
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<TD align=right>
<font face=arial size=2>card Number: </font>
</td>
<td>
<asp:textbox Id=ccnum Runat=server/>
</td>
<td>
<asp:requiredfieldvalidator id= "Ccnumreqval"
Controltovalidate= "Ccnum"
errormessage= "credit card number. "
Display= "Dynamic"
Font-name= "Verdana" font-size= "12"
runat=server>
*
</asp:RequiredFieldValidator>
<asp:regularexpressionvalidator id= "Ccnumcustval"
Controltovalidate= "Ccnum"
Display= "Static"
validationexpression= "^\d{15}$"
Font-name= "Arial" font-size= "11"
runat=server>
Wrong credit card number
</asp:RegularExpressionValidator>
</td>
</tr>
</table>
<p>
<input runat= "Server" Type=submit value= "registration" >
<p>
</form>
</center>



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.