📜  ASP.Net Web表单必填字段验证器

📅  最后修改于: 2020-12-28 00:40:10             🧑  作者: Mango

ASP.NET RequiredFieldValidator控件

该验证器用于使输入控制成为必需。如果用户将输入控件留空,则会引发错误。

它用于强制执行所需的表单控制并限制用户提供数据。

注意:它会在执行验证之前从输入值的开头和结尾删除多余的空格。

应设置ControlToValidateproperty的控件ID以进行验证。

RequiredFieldValidator属性

Property Description
AccessKey It is used to set keyboard shortcut for the control.
BackColor It is used to set background color of the control.
BorderColor It is used to set border color of the control.
Font It is used to set font for the control text.
ForeColor It is used to set color of the control text.
Text It is used to set text to be shown for the control.
ToolTip It displays the text when mouse is over the control.
Visible To set visibility of control on the form.
Height It is used to set height of the control.
Width It is used to set width of the control.
ErrorMessage It is used to set error message that display when validation fails.
ControlToValidate It takes ID of control to validate.

在这里,在下面的示例中,我们将解释RequiredFieldValidator控件并创建强制性TextBox控件。

// RequiredFieldValidator.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RequiredFieldValidator.aspx.cs" 
Inherits="asp.netexample.RequiredFieldValidator" %>







User Name
Password
 

输出:

在浏览器中查看时,它将产生以下输出。

当用户使用空控件登录时,它将引发错误消息。