RequiredFieldValidator Control (General Reference) 

Evaluates the value of an input control to ensure that the user enters a value.

<asp:RequiredFieldValidator
    AccessKey="string"
    AssociatedControlID="string"
    BackColor="color name|#dddddd"
    BorderColor="color name|#dddddd"
    BorderStyle="NotSet|None|Dotted|Dashed|Solid|Double|Groove|Ridge|
        Inset|Outset"
    BorderWidth="size"
    ControlToValidate="string"
    CssClass="string"
    Display="None|Static|Dynamic"
    EnableClientScript="True|False"
    Enabled="True|False"
    EnableTheming="True|False"
    EnableViewState="True|False"
    ErrorMessage="string"
    Font-Bold="True|False"
    Font-Italic="True|False"
    Font-Names="string"
    Font-Overline="True|False"
    Font-Size="string|Smaller|Larger|XX-Small|X-Small|Small|Medium|
        Large|X-Large|XX-Large"
    Font-Strikeout="True|False"
    Font-Underline="True|False"
    ForeColor="color name|#dddddd"
    Height="size"
    ID="string"
    InitialValue="string"
    OnDataBinding="DataBinding event handler"
    OnDisposed="Disposed event handler"
    OnInit="Init event handler"
    OnLoad="Load event handler"
    OnPreRender="PreRender event handler"
    OnUnload="Unload event handler"
    runat="server"
    SetFocusOnError="True|False"
    SkinID="string"
    Style="string"
    TabIndex="integer"
    Text="string"
    ToolTip="string"
    ValidationGroup="string"
    Visible="True|False"
    Width="size"
/>

Remarks

Use the RequiredFieldValidator control to make an input control a mandatory field. The input control fails validation if the value it contains does not change from its initial value when validation is performed. This prevents the user from leaving the associated input control unchanged. By default, the initial value is an empty string (""), which indicates that a value must be entered in the input control for it to pass validation.

NoteNote

Extra spaces at the beginning and end of the input value are removed before validation is performed. This prevents a space entered in the input control from passing validation.

Sometimes, you will want to have an initial value that is not an empty string. This is useful when you have a default value for an input control and want the user to select a different value. For example, you can have a ListBox control with an entry selected, by default, that contains instructions to the user to select an item from the list. The user must select an item from the control, but you do not want the user to select the item containing the instructions. You can prevent the user from selecting this item by specifying its value as the initial value. If the user selects this item, the RequiredFieldValidator control displays its error message. To specify the starting value of the associated input control, set the InitialValue property.

NoteNote

The InitialValue property does not set the default value for the input control. The InitialValue property does not even need to match the default value of the input control. It simply indicates the value that you do not want the user to enter in the input control. The input control fails validation if it contains this value when validation is performed.

Multiple validators can be associated with the same input control. For example, a RequiredFieldValidator control can be used to ensure input to a control, while at the same time, a RangeValidator control can be used to ensure that the input is within a specified data range.

For more information about the RequiredFieldValidator control, see the RequiredFieldValidator class.

Example

The following code example demonstrates how to use the RequiredFieldValidator control to make a TextBox control a mandatory field.

Security noteSecurity Note

This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

Name: 
 <asp:TextBox id="Text1" 
      Text="Enter a value" 
      runat="server"/>

 <asp:RequiredFieldValidator id="RequiredFieldValidator1"  
      ControlToValidate="Text1"
      Text="Required Field!" 
      runat="server"/>

 <p />
   
 <asp:Button id="Button1" 
      runat="server" 
      Text="Validate"/>
Name: 
 <asp:TextBox id="Text1" 
      Text="Enter a value" 
      runat="server"/>

 <asp:RequiredFieldValidator id="RequiredFieldValidator1"  
      ControlToValidate="Text1"
      Text="Required Field!" 
      runat="server"/>

 <p />
   
 <asp:Button id="Button1" 
      runat="server" 
      Text="Validate"/>

See Also

Reference

RequiredFieldValidator

Other Resources

Validation Server Control Syntax