ASP.NET RequiredFieldValidator Control

This validator is used to make an input control required. It will throw an error if user leaves input control empty.

It is used to mandate form control required and restrict the user to provide data.

Note: It removes extra spaces from the beginning and end of the input value before validation is performed.

The ControlToValidateproperty should be set with the ID of control to validate.

RequiredFieldValidator Properties

PropertyDescription
AccessKeyIt is used to set keyboard shortcut for the control.
BackColorIt is used to set background color of the control.
BorderColorIt is used to set border color of the control.
FontIt is used to set font for the control text.
ForeColorIt is used to set color of the control text.
TextIt is used to set text to be shown for the control.
ToolTipIt displays the text when mouse is over the control.
VisibleTo set visibility of control on the form.
HeightIt is used to set height of the control.
WidthIt is used to set width of the control.
ErrorMessageIt is used to set error message that display when validation fails.
ControlToValidateIt takes ID of control to validate.

Example

Here, in the following example, we are explaining RequiredFieldValidator control and creating to mandatory TextBox controls.

// RequiredFieldValidator.aspx

Output:

It produces the following output when view in the browser.

ASP Required 1

It throws error messages when user login with empty controls.

ASP Required 2