ASP.NET RequiredFieldValidator Control
Validation Server Controls
Definition and Usage
The RequiredFieldValidator control is used to make an input control a required (mandatory) field.
With this control, validation fails if the initial value of the input value is unchanged. By default, the initial value is an empty string ("").
**Note:** Leading and trailing spaces in 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.
property
| property |
Description |
| BackColor |
The background color of the RequiredFieldValidator control. |
| ControlToValidate |
The id of the control to be validated. |
| Display |
The display behavior of the validation control. Valid values are:
|
None - The control is not displayed. Used only to display an error message in the ValidationSummary control.
Static - If validation fails, the control displays the error message. Even if the input passes validation, space for the message is reserved on the page (i.e., the space for displaying the message is pre-allocated).
Dynamic - If validation fails, the control displays the error message. If the input passes validation, no space for 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 (starting) value of the input control. The default is "". | | IsValid | A Boolean value that indicates whether the control specified by ControlToValidate 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, 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 (mandatory) field.
Validation Server Controls