Latest web development tutorials

ASP.NET RequiredFieldValidator Control

Validation Server Controls Validation Server Controls

Definition and Usage

RequiredFieldValidator control is used as a control input required (mandatory) fields.

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

Note: The leading and trailing spaces of the input value will be removed prior to verification.

Note: InitialValue property is not set default values for the input control.It indicatesthat you do not wantuserstoenter values in the input control.


Attributes

属性 描述
BackColor RequiredFieldValidator 控件的背景颜色。
ControlToValidate 要验证的控件的 id。
Display 验证控件的显示行为。合法值有:
  • None - 控件不显示。仅用于 ValidationSummary 控件中显示错误消息。
  • Static - 如果验证失败,控件显示错误消息。即使输入通过验证,也在页面上预留显示消息的空间,即用于显示消息的空间是预先分配好的。
  • Dynamic - 如果验证失败,控件显示错误消息。如果输入通过验证,页面上不预留显示消息的空间,即用于显示消息的空间是动态添加的。
EnableClientScript 布尔值,规定是否启用客户端验证。
Enabled 布尔值,规定是否启用验证控件。
ErrorMessage 当验证失败时,在 ValidationSummary 控件中显示的文本。 注释:如果未设置 Text 属性,文本也会显示在验证控件中。
ForeColor 控件的前景颜色。
id 控件的唯一 id。
InitialValue 定输入控件的初始值(开始值)。默认是 ""。
IsValid 布尔值,指示由 ControlToValidate 指定的控件是否通过验证。
runat 规定该控件是一个服务器控件。必须设置为 "server"。
Text 当验证失败时显示的消息。


Examples

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


Validation Server Controls Validation Server Controls