Microsoft KB Archive/313044

= HOW TO: Use the RequiredFieldValidator Control with Other Validation Controls to Handle Blank Entries =

Article ID: 313044

Article Last Modified on 8/28/2007

-

APPLIES TO


 * Microsoft ASP.NET 1.1
 * Microsoft ASP.NET 1.0

-



This article was previously published under Q313044



IN THIS TASK
SUMMARY
 * Requirements
 * Create an ASP.NET Web Application
 * Create the Validation Sample

REFERENCES



SUMMARY
This step-by-step article describes how to use the RequiredFieldValidator control to handle blank entries when you use the other validation controls that are available with ASP.NET.

All validation controls except RequiredFieldValidator report a blank entry as valid input. If you want to prevent blank entries with validation controls, you must use a RequiredFieldValidator control. This adds to the overall flexibility of the validation controls, and allows you to provide a different error message to address blank entries.

back to the top

Requirements
The following items describe the recommended hardware, software, network infrastructure, skills and knowledge, and service packs that you need:
 * Microsoft Windows 2000 Professional, Microsoft Windows 2000 Server, Microsoft Windows 2000 Advanced Server, or Microsoft Windows XP
 * Microsoft .NET Framework
 * Microsoft Internet Information Services (IIS)

back to the top

Create a Sample ASP.NET Web Application
To create a new ASP.NET Web application named ValidatorSample, follow these steps:
 * 1) Start Microsoft Visual Studio .NET.
 * 2) On the File menu, point to New, and then click Project.
 * 3) In the New Project dialog box, click Visual C# Projects under Project Type, and then click ASP.NET Web Application under Templates.NOTE: If you want to create a Visual Basic ASP.NET Web Application project, select Visual Basic under Project Type. The rest of the steps in this article are the same for either project type.


 * 1) In the Location text box, replace WebApplication# in the URL path with ValidatorSample . If you are using the local server, you can leave the server name as http://localhost. If you do this, the Location text box entry is http://localhost/ValidatorSample.

back to the top

Create the Validation Sample
NOTE: When you use several validation controls on a Web Form, you can use a Validation Summary control to display any error messages in a central location. In this way, you can give the page a much cleaner presentation.

To create a sample Web Form page that demonstrates how to use the RequiredFieldValidator control as a solution to handle blank entries when you use the other available validation controls, follow these steps:  Add a new Web Form to the Validatorpage.aspx project. To do this, right-click the project node in Solution Explorer, click Add, and then click Add WebForm. Name the .aspx page Validatorpage.aspx, and then click Open. With the .aspx page open in Design view, drag two TextBox controls from the Web Forms section of the toolbar onto the page. Drag a Web Form Button control onto the page. Drag a CompareValidator control onto the page. Modify the properties of the control as follows:  In the Properties pane, change ControlToValidate to TextBox2. Change ControlToCompare to TextBox1. Type Invalid Value for CompareValidator! for the ErrorMessage property.</li></ol> </li> On the File menu, click Save All to save the Web Form and other associated project files.</li> On the Build menu in the Visual Studio .NET integrated development environment (IDE), click Build to build the project.</li> Right-click the Web Form page, and then click View In Browser.</li> With the page displayed in the browser, type Hello in both TextBox1 and TextBox2, and then click Button1. Because this is considered valid input for TextBox2, no error message is displayed.</li> Leave the word &quot;Hello&quot; in TextBox1, type Goodbye in TextBox2, and then click Button1. The CompareValidator control displays error message text to show that the value for TextBox2 is not valid when compared to TextBox1.</li> Clear TextBox2, and then click Button1. Although you may expect to receive an error message, the blank entry is reported as valid.</li> Switch back to Design view in the Visual Studio .NET editor. In the Web Forms section of the toolbox, drag a RequiredFieldValidator control onto the page. In the Properties pane, set the ControlToValidate property to TextBox2, and then type Entry Required! for the ErrorMessage property.</li> On the File menu, click Save All to save the Web Form and other associated project files.</li> On the Build menu in the Visual Studio .NET IDE, click Build to build the project.</li> Right-click the Web Form page, and then click View In Browser.</li> With the page displayed in the browser, type Hello in TextBox1 and leave TextBox2 blank. Click Button1. Because you have added the RequiredFieldValidator control, you receive the &quot;Entry Required!&quot; error message.</li></ol>

back to the top