ASP.NET RequiredFieldValidator Control

Definition and Usage

The RequiredFieldValidator control is used to make an input control a required field.

Through this control, the validation will fail if the initial value of the input is not changed. By default, the initial value is an empty string ("

Note:Leading and trailing spaces of the input value will be removed before validation.

Note:InitialValue does not set a default value for the input control. It indicates that youNot desiredThe value entered by the user in the input control.

Property

Property Description
BackColor Background color of the RangeValidator control
ControlToValidate The id of the control to be validated
Display

Display behavior of the validation control.

Valid values include:

  • None - Validation messages are never displayed inline.
  • Static - Space allocated for displaying validation messages in the page layout.
  • Dynamic - A space for displaying validation messages is dynamically added to the page if validation fails.
EnableClientScript Boolean value specifying whether client-side validation is enabled.
Enabled Boolean value specifying whether the validation control is enabled.
ErrorMessage

Text displayed in the ValidationSummary control when validation fails.

Note: If the Text property is not set, the text will also be displayed in the validation control.

ForeColor The foreground color of the control.
id The unique id of the control.
InitialValue Specifies the initial value (starting value) of the input control. The default is "".
IsValid Boolean value indicating whether the associated input control passes validation.
runat Specifies that the control is a server control. Must be set to 'server'.
Text Message displayed when validation fails.

Example

RequiredFieldValidator
In this example, we declare two TextBox controls, one Button control, and one RequiredFieldValidator control in the .aspx file. It demonstrates how to use the RequiredFieldValidator control to make the 'txt_name' text box a required field.