ASP.NET RequiredFieldValidatorControl
Validation 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:
|
| 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 Control Other Extensions