更新:2007 年 11 月
驗證相關輸入控制項的值是否符合由規則運算式 (Regular Expression) 指定的比對模式。
命名空間:
System.Web.UI.WebControls 組件:
System.Web (在 System.Web.dll 中)
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public Class RegularExpressionValidator _
Inherits BaseValidator
Dim instance As RegularExpressionValidator
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class RegularExpressionValidator : BaseValidator
[AspNetHostingPermissionAttribute(SecurityAction::LinkDemand, Level = AspNetHostingPermissionLevel::Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction::InheritanceDemand, Level = AspNetHostingPermissionLevel::Minimal)]
public ref class RegularExpressionValidator : public BaseValidator
/** @attribute AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal) */
/** @attribute AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal) */
public class RegularExpressionValidator extends BaseValidator
public class RegularExpressionValidator extends BaseValidator
<asp:RegularExpressionValidator />
RegularExpressionValidator 控制項會檢查輸入控制項的值是否符合規則運算式所定義的比對模式。這種類型的驗證可讓您檢查可預測的字元序列 (Sequence),例如:電子郵件地址、電話號碼和郵遞區號。
除非瀏覽器不支援用戶端驗證或明確停用用戶端驗證 (將 EnableClientScript 屬性設定為 false),否則執行伺服器端和用戶端驗證。
規則運算式驗證的實作在用戶端和伺服器上略有不同。在用戶端,使用 JScript 規則運算式語法。在伺服器上,使用 System.Text.RegularExpressions..::.Regex 語法。因為 JScript 規則運算式語法是 System.Text.RegularExpressions..::.Regex 語法的子集合,建議使用 JScript 規則運算式語法,以便在用戶端和伺服器兩邊產生相同的結果。
如需驗證控制項的詳細資訊,請參閱 BaseValidator。如需規則運算式的詳細資訊,請參閱 .NET Framework 規則運算式。
可及性
根據預設為這個控制項呈現的標記,可能與可及性標準 (例如 Web 內容可及性方針 1.0 (WCAG) 優先權 1 方針) 不一致。如需這個控制項之可及性支援的詳細資料,請參閱 ASP.NET 控制項和網頁可及性。
下列範例中,示範如何使用 RegularExpressionValidator 控制項以驗證 5 碼郵遞區號。
安全性注意事項: |
|---|
這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。如需詳細資訊,請參閱 指令碼攻擊概觀。 |
<%@ Page Language="VB" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head>
<title>RegularExpressionValidator Example</title>
<script runat="server">
Sub ValidateBtn_Click(sender As Object, e As EventArgs)
If Page.IsValid Then
lblOutput.Text = "Page is Valid."
Else
lblOutput.Text = "Page is InValid."
End If
End Sub
</script>
</head>
<body>
<h3>RegularExpressionValidator Example</h3>
<br />
<form id="form1" runat="server">
<table style="background-color:#eeeeee; padding:10">
<tr valign="top">
<td colspan="3">
<asp:Label ID="lblOutput"
Text="Enter a 5-digit ZIP Code"
runat="server"
AssociatedControlID="TextBox1"/>
</td>
</tr>
<tr>
<td colspan="3">
<b>Personal Information</b>
</td>
</tr>
<tr>
<td align="right">
Zip Code:
</td>
<td>
<asp:TextBox id="TextBox1"
runat="server"/>
</td>
<td>
<asp:RegularExpressionValidator id="RegularExpressionValidator1"
ControlToValidate="TextBox1"
ValidationExpression="\d{5}"
Display="Static"
ErrorMessage="Zip code must be 5 numeric digits"
EnableClientScript="False"
runat="server"/>
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button text="Validate"
OnClick="ValidateBtn_Click"
runat="server" />
</td>
<td></td>
</tr>
</table>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head>
<title>RegularExpressionValidator Example</title>
<script runat="server">
void ValidateBtn_Click(Object sender, EventArgs e)
{
if (Page.IsValid)
{
lblOutput.Text = "Page is Valid.";
}
else
{
lblOutput.Text = "Page is InValid.";
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<h3>RegularExpressionValidator Example</h3>
<table style="background-color:#eeeeee; padding:10">
<tr valign="top">
<td colspan="3">
<asp:Label ID="lblOutput"
Text="Enter a 5-digit ZIP Code"
runat="server"
AssociatedControlID="TextBox1"/>
</td>
</tr>
<tr>
<td colspan="3">
<b>Personal Information</b>
</td>
</tr>
<tr>
<td align="right">
Zip Code:
</td>
<td>
<asp:TextBox id="TextBox1"
runat="server"/>
</td>
<td>
<asp:RegularExpressionValidator id="RegularExpressionValidator1"
ControlToValidate="TextBox1"
ValidationExpression="\d{5}"
Display="Static"
ErrorMessage="ZIP code must be 5 numeric digits"
EnableClientScript="False"
runat="server"/>
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button text="Validate"
OnClick="ValidateBtn_Click"
runat="server" />
</td>
<td></td>
</tr>
</table>
</form>
</body>
</html>
<%@ Page Language="JScript" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head>
<title>RegularExpressionValidator Example</title>
<script runat="server">
function ValidateBtn_Click(sender : Object, e : EventArgs)
{
if (Page.IsValid)
{
lblOutput.Text = "Page is Valid.";
}
else
{
lblOutput.Text = "Page is InValid.";
}
}
</script>
</head>
<body>
<h3>RegularExpressionValidator Example</h3>
<br />
<form id="form1" runat="server">
<table style="background-color:#eeeeee; padding:10">
<tr valign="top">
<td colspan="3">
<asp:Label ID="lblOutput"
Text="Enter a 5-digit ZIP Code"
runat="server"
AssociatedControlID="TextBox1"/>
</td>
</tr>
<tr>
<td colspan="3">
<b>Personal Information</b>
</td>
</tr>
<tr>
<td align="right">
Zip Code:
</td>
<td>
<asp:TextBox id="TextBox1"
runat="server"/>
</td>
<td>
<asp:RegularExpressionValidator id="RegularExpressionValidator1"
ControlToValidate="TextBox1"
ValidationExpression="\d{5}"
Display="Static"
ErrorMessage="ZIP Code must be 5 numeric digits"
EnableClientScript="False"
runat="server"/>
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button text="Validate"
OnClick="ValidateBtn_Click"
runat="server" />
</td>
<td></td>
</tr>
</table>
</form>
</body>
</html>
System..::.Object
System.Web.UI..::.Control
System.Web.UI.WebControls..::.WebControl
System.Web.UI.WebControls..::.Label
System.Web.UI.WebControls..::.BaseValidator
System.Web.UI.WebControls..::.RegularExpressionValidator
這個型別的任何 Public static (在 Visual Basic 中為 Shared) 成員都具備執行緒安全。並非所有的執行個體成員都是安全執行緒。
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
.NET Framework 和 .NET Compact Framework 並不支援各種平台的所有版本。如需支援平台版本的相關資訊,請參閱 .NET Framework 系統需求。
.NET Framework
支援版本:3.5、3.0、2.0、1.1、1.0
參考
其他資源