Microsoft KB Archive/316662

= How to use ASP.NET validation controls from Visual Basic .NET or from Visual Basic 2005 =

Article ID: 316662

Article Last Modified on 2/12/2007

-

APPLIES TO


 * Microsoft ASP.NET 1.0
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Studio .NET 2002 Professional Edition
 * Microsoft Visual Studio .NET 2002 Enterprise Architect
 * Microsoft Visual Studio .NET 2002 Enterprise Developer
 * Microsoft Visual Studio .NET 2002 Academic Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic 2005
 * Microsoft Visual Studio .NET 2003 Academic Edition
 * Microsoft Visual Studio .NET 2003 Enterprise Architect
 * Microsoft Visual Studio .NET 2003 Enterprise Developer
 * Microsoft Visual Studio .NET 2003 Professional Edition
 * Microsoft Visual Studio 2005 Professional Edition
 * Microsoft Visual Studio 2005 Standard Edition

-



This article was previously published under Q316662



IN THIS TASK
SUMMARY
 * Requirements
 * Use ASP.NET Validation Controls from Visual Studio .NET
 * Verification

REFERENCES



SUMMARY
The .NET Framework contains a variety of validation controls that, when placed on an ASP.NET Web Forms page, validate user input entered into the control fields and display associated error messages for each control. This article explains how you can use ASP.NET validation controls to quickly and easily implement client-side and server-side validation of user input.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
 * Visual Studio .NET or Visual Studio 2005
 * Microsoft Internet Information Server (IIS) 5.0 or later

This article assumes that you are familiar with the following topics:
 * Web applications
 * ASP.NET

back to the top

Use ASP.NET Validation Controls from Visual Studio .NET
Validation of user input can be a cumbersome task. The .NET Framework provides validation controls that validate user input and display appropriate error messages whenever invalid data is encountered in a validation control. This becomes a big time saver when you need to duplicate this validation on both the client and server. Additionally, a ValidationSummary control is provided to display all error messages for a page in one area of the screen.

The following steps create an ASP.NET Web application that prompts the user for a user name, e-mail address, and password. When the user submits the requested information, validation controls on the form validate the data entered by the user and display any error messages in a summarized list at the bottom of the screen.  Start Visual Studio .NET or Visual Studio 2005. Create a new ASP.NET Web application project in Visual Basic .NET or in Visual Basic 2005. Switch to the HTML view of the WebForm1.aspx window.  Add input controls and associated messages (prompts) to your form to solicit input from the user. In the WebForm1 HTML window, copy and paste the following code between the opening and closing form tags.

NOTE: When you paste code into the HTML window, it is important to paste the code segments as HTML. To do this, select Paste as HTML on the shortcut menu.   RequiredFieldValidator controls verify that some value has been entered for the field that the control specifies. Add RequiredFieldValidator controls to your form for the UserName and Password fields.

In the WebForm1 HTML window, copy and paste the following code after the tag:    </li>  Password fields are generally verified by forcing the user to type the same password twice. The CompareValidator control compares the contents of two input fields and generates an error message if they do not match. Use a CompareValidator control to validate the password fields.

In the WebForm1 HTML window, copy and paste the following code after the validation controls added in step 5: <asp:CompareValidator id=valComparePassword ControlToValidate=txtConfirmPassword ErrorMessage=&quot;Password fields must match.&quot; ControlToCompare=txtPassword Display=None EnableClientScript=true Runat=server/> </li>  There are fields that sometimes require more customized validation, such as an e-mail address field. The RegularExpressionValidator control ensures that the basic format of @ .com is followed. The contents of the field are tested against a regular expression, and if no match to the expression is made, the user receives an error message. Add a RegularExpressionValidator control to validate the format of the e-mail address provided by the user.

In the WebForm1 HTML window, copy and paste the following code after the preceding validation controls: <asp:RegularExpressionValidator ID=valEmailAddress ControlToValidate=txtEmail ValidationExpression=&quot;.*@.*\..*&quot; ErrorMessage=&quot;Email address is invalid.&quot; Display=None EnableClientScript=true Runat=server/> </li>  Add a Submit button to allow the user to submit the page to the server and validate the content of the controls on the form.

In the WebForm1 HTML window, copy and paste the following code after the preceding validation controls: <input type=submit id=cmdSumbit value=submit runat=server/> </li>  Finally, a ValidationSummary control is used to display all encountered errors in a single area of the form.

In the WebForm1 HTML window, copy and paste the following code after the code for the Submit button: <asp:ValidationSummary id=ValSummary HeaderText=&quot;The following errors were found:&quot; ShowSummary=True DisplayMode=List Runat=server/> </li> Click Save.</li> On the Debug menu, click Start to build and run the application.

Form1 is displayed on the screen.</li></ol>

back to the top

Verification

 * If the user clicks Submit without entering any input values, the three required-field error messages should be displayed.
 * If the user submits two password values that do not match, the &quot;Password fields must match&quot; error message should be displayed.
 * If the user submits an e-mail address that does not conform to the proper format, the &quot;E-mail address is invalid&quot; error message should be displayed.

back to the top

<div class="references_section">