Search - Articles - Dev Forums - Favorites - Member Login
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009


Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

FREE 12 month online training for ASP.NET & MS Expression Studio and a Free copy of MS Expression Web with Windows Server Purchase
RequiredFieldValidator Control in ASP.Net

Author: Faraz

In this article you will see the technique of validating the input made by end user. This can be done by using RequiredFieldValidator Control.

When you develop an ASP.Net form you will find that certain fields on that form must be entered by visitors. For example, you may have a form that requires visitors to enter a password or maybe you have a form where visitors need to enter a shipping address. Both of those fields would be required.

 

You require a field to have data in it on an ASP.Net page by using a RequiredFieldValidator control. That control requires visitors to enter something into the fields before submitting the form.

 

The web form used here shows you how to use that control. The page asks the visitors to enter their name and then press the OK button. If they leave the field blank, the form is not submitted to the server. Instead they see an error message letting them know that they must enter their name.

 

Add the following code on to your page:

 

<asp:TextBox Text="Enter Your Name" id="txtName" runat="server" onFocus="this.value = ''">asp:TextBox>

<asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ErrorMessage="Invalid Input" ControlToValidate="txtName">

asp:RequiredFieldValidator><BR>

<asp:Button id="btnOK" runat="server" Text="OK">asp:Button>

 

The TextBox control is used by the end user to enter the name and on the button click the RequiredFieldValidator control is called and check for input. If the input is valid the form is processed, otherwise, it will report an error.

 

Note: You have to take care for two things:

  • Specify the control which you want to validate using ControlToValidate property. Here I am using TextBox control.
  • If you want to reserve the space for an error message when the error is generated, then set the Display property to Dynamic else you can use Static.
   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Checking the range using Range Validator Control in ASP.Net

Disclaimer - Privacy
© 2002-2010 DevASP.net