ASP.NET RequiredFieldValidatorControl


Validation Server ControlsValidation Server Control

Definition and Usage

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

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

Note:Spaces at the beginning and end of the input value are removed before validation.

Note:The InitialValue property does not set a default value for the input control. It indicates the value that youdo not wantthe 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. Legal values are:
  • None - The control is not displayed. Used only in the ValidationSummary control to display error messages.
  • Static - If validation fails, the control displays an error message. Even if the input passes validation, space for displaying the message is reserved on the page, i.e., the space for displaying the message is preallocated.
  • Dynamic - If validation fails, the control displays an error message. If the input passes validation, no space for displaying the message is reserved on the page, i.e., the space for displaying the message is added dynamically.
EnableClientScript A Boolean value that specifies whether client-side validation is enabled.
Enabled A Boolean value that specifies whether the validation control is enabled.
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 The unique id of the control.
InitialValue Specifies the initial value (starting value) of the input control. Default is "".
IsValid A Boolean value that indicates whether the control specified by ControlToValidate passes 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, a Button control, and a RequiredFieldValidator control in the .aspx file. It demonstrates how to use the RequiredFieldValidator control to make the "txt_name" text box a required field.


Validation Server ControlsValidation Server Control Other Extensions