ASP.NET RequiredFieldValidator Control

ASP.NET Tutorial

WP Tutorial

WP Reference Manual

ASP.NET Razor

ASP.NET MVC

WF Tutorial

WF Reference Manual

ASP.NET RequiredFieldValidator Control


Validation Server Controls


Definition and Usage

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

The validation fails if the initial value of the input is not changed. By default, the initial value is an empty string ("").

Note: Whitespace at the beginning and end of the input value is removed before validation.

Note: The InitialValue property does not set a default value for the input control. It indicates a value that you do not want the user to enter in the input control.


Properties

Property Description
BackColor The background color of the RequiredFieldValidator control.
ControlToValidate The id of the control to validate.
Display The display behavior of the validation control. Valid values are:
  • None - The control is not displayed. Used only for displaying error messages in the ValidationSummary control.
  • Static - If validation fails, the control displays the error message. Space for displaying the message is reserved on the page even if the input passes validation.
  • Dynamic - If validation fails, the control displays the error message. If the input passes validation, no space is reserved on the page for displaying the message.
EnableClientScript Boolean value that specifies whether to enable client-side validation.
Enabled Boolean value that specifies whether to enable the validation control.
ErrorMessage The text displayed in the ValidationSummary control when validation fails.
Note: If the Text property is not set, the text is also displayed in the validation control.
ForeColor The foreground color of the control.
id A unique id for the control.
InitialValue The initial (starting) value for the input control. The default is "".
IsValid Boolean value indicating whether the control specified by ControlToValidate has passed validation.
runat Specifies that the control is a server control. Must be set to "server".
Text The message displayed when validation fails.

Example

RequiredFieldValidator

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


Validation Server Controls