RegularExpressionAttribute 類別

定義

規定 ASP.NET 動態資料中的資料欄位值必須與指定的正則表達式相符。

public ref class RegularExpressionAttribute : System::ComponentModel::DataAnnotations::ValidationAttribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property, AllowMultiple=false)]
public class RegularExpressionAttribute : System.ComponentModel.DataAnnotations.ValidationAttribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple=false)]
public class RegularExpressionAttribute : System.ComponentModel.DataAnnotations.ValidationAttribute
[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property, AllowMultiple=false)>]
type RegularExpressionAttribute = class
    inherit ValidationAttribute
[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple=false)>]
type RegularExpressionAttribute = class
    inherit ValidationAttribute
Public Class RegularExpressionAttribute
Inherits ValidationAttribute
繼承
RegularExpressionAttribute
屬性

範例

以下範例展示了如何利用該 RegularExpressionAttribute 屬性來驗證 FirstName 和 LastName 資料欄位。 正則表達式最多可容納40個大寫與小寫字元。 此範例會執行下列工作:

  • 實作一個元資料部分類別及其相關的元資料類別。
  • 在相關的元資料類別中,將屬性 RegularExpressionAttribute 套用到FirstName和LastName資料欄位,指定模式與自訂錯誤訊息。
using System;
using System.Web.DynamicData;
using System.ComponentModel.DataAnnotations;

[MetadataType(typeof(CustomerMetaData))]
public partial class Customer
{
}

public class CustomerMetaData
{
   
    // Allow up to 40 uppercase and lowercase 
    // characters. Use custom error.
    [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$", 
         ErrorMessage = "Characters are not allowed.")]
    public object FirstName;

    // Allow up to 40 uppercase and lowercase 
    // characters. Use standard error.
    [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$")]
    public object LastName;
}
Imports System.Web.DynamicData
Imports System.ComponentModel.DataAnnotations


<MetadataType(GetType(CustomerMetaData))> _
Partial Public Class Customer

    
End Class

Public Class CustomerMetaData
    
    ' Allow up to 40 uppercase and lowercase 
    ' characters. Use custom error.
    <RegularExpression("^[a-zA-Z''-'\s]{1,40}$", _
                       ErrorMessage:="Characters are not allowed.")> _
    Public FirstName As Object

    ' Allow up to 40 uppercase and lowercase 
    ' characters. Use standard error.
    <RegularExpression("^[a-zA-Z''-'\s]{1,40}$")> _
    Public LastName As Object
End Class

備註

當你需要驗證屬性值與正規表達式的對照時,你會套用 RegularExpressionAttribute 屬性給屬性。 正則表達式讓你能非常精確地指定有效值的格式。 Pattern 屬性包含正則表達式。 若屬性值為空或為空字串(“”),該值會自動通過 RegularExpressionAttribute 屬性的驗證。 要驗證該值是否為 null 空字串,請使用 RequiredAttribute 屬性。

正則表達式搜尋完全匹配的,不使用 ^ 模式的前後 $ 與結束時,結果與使用它相同的結果。 搜尋結果時,在前 .*加並加上 。

建構函式

名稱 Description
RegularExpressionAttribute(String)

初始化 RegularExpressionAttribute 類別的新執行個體。

屬性

名稱 Description
ErrorMessage

若驗證失敗,會接收或設定錯誤訊息以關聯驗證控制項。

(繼承來源 ValidationAttribute)
ErrorMessageResourceName

取得或設定錯誤訊息資源名稱,以便在驗證失敗時查詢 ErrorMessageResourceType 屬性值。

(繼承來源 ValidationAttribute)
ErrorMessageResourceType

取得或設定資源類型,以便在驗證失敗時用於錯誤訊息查詢。

(繼承來源 ValidationAttribute)
ErrorMessageString

會收到本地化驗證錯誤訊息。

(繼承來源 ValidationAttribute)
MatchTimeoutInMilliseconds

取得或設定執行單一匹配操作的時間(以毫秒計),在該操作逾時前完成。

Pattern

得到正則表達式模式。

RequiresValidationContext

會得到一個值,表示該屬性是否需要驗證上下文。

(繼承來源 ValidationAttribute)
TypeId

在衍生類別中實作時,取得這個 Attribute的唯一標識碼。

(繼承來源 Attribute)

方法

名稱 Description
Equals(Object)

傳回值,這個值表示這個實例是否等於指定的物件。

(繼承來源 Attribute)
FormatErrorMessage(String)

格式化錯誤訊息,若正則表達式驗證失敗時會顯示。

GetHashCode()

傳回這個實例的哈希碼。

(繼承來源 Attribute)
GetType()

取得目前實例的 Type

(繼承來源 Object)
GetValidationResult(Object, ValidationContext)

檢查指定值是否相較於當前驗證屬性有效。

(繼承來源 ValidationAttribute)
IsDefaultAttribute()

在衍生類別中覆寫時,指出這個實例的值是否為衍生類別的預設值。

(繼承來源 Attribute)
IsValid(Object, ValidationContext)

驗證指定值相對於目前驗證屬性的值。

(繼承來源 ValidationAttribute)
IsValid(Object)

檢查使用者輸入的值是否符合正則表達式模式。

Match(Object)

在衍生類別中覆寫時,傳回值,指出這個實例是否等於指定的物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)
Validate(Object, String)

驗證指定的物件。

(繼承來源 ValidationAttribute)
Validate(Object, ValidationContext)

驗證指定的物件。

(繼承來源 ValidationAttribute)

明確介面實作

名稱 Description
_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取 物件的型別資訊,可用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開屬性和方法的存取權。

(繼承來源 Attribute)

適用於

另請參閱