Compartir a través de


RequiredFieldValidator Control

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

<asp:RequiredFieldValidator 
     id="ProgrammaticID" 
     ControlToValidate="ProgrammaticID of control to validate" 
     InitialValue="value"
     ErrorMessage="Message to display in ValidationSummary control"
     Text="Message to display in control"
     ForeColor="value" 
     BackColor="value"  
     runat="server" >
</asp:RequiredFieldValidator>

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.

Note   Extra spaces at the beginning and end of the input value are removed before validation is performed. This prevents a space being 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 displays its error message. To specify the starting value of the associated input control, set the InitialValue property.

Note   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 can be used to ensure input to a control, while at the same time, a RangeValidator can be used to ensure that the input is within a specified data range.

For detailed information on the RequiredFieldValidator control, see the RequiredFieldValidator class.

Example

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

<html>
<body>

   <h3>RequiredFieldValidator Example</h3>

   <form runat="server">

      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"/>

   </form>

</body>
</html>

See also

RequiredFieldValidator Class | Validation Server Controls