How to use RequiredFieldValidator


One of the main tasks for every web developer is to check for errors while developing web pages. According to the server side terminology, this concept is called as Validation.This is one of the important controls used in majority of web applications.

Use of RequiredFieldValidator

If a user fails to enter the required information on the form then the message given on this validation control will appear thereby forcing the user to fill up the required information.The RequiredFieldValidator control is used to make an input control a required field.With this control, the validation fails if the input value does not change from its initial value. By default, the initial value is an empty string ("").

Important Properties



ControlToValidate   

The id of the control to validate

ErrorMessage   

The text to display in the ValidationSummary control when validation fails. 
Note:This text will also be displayed in the validation control if the Text property is not set.

Text  

The message to display when validation fails


The code given below will illustrate this concept in detail

<p>
Name:
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ErrorMessage="Please enter your name" ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
</p>
<p>
Age:&nbsp;&nbsp;&nbsp;
<asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator id="RequiredFieldValidator2" runat="server" ErrorMessage="Please enter your age" ControlToValidate="TextBox2"></asp:RequiredFieldValidator>


 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Best Hostgator Coupon Code