RegularExpressionAttribute 類別

定義

指定 ASP.NET Dynamic Data 中的資料欄位值必須符合指定的規則運算式 (Regular Expression)。Specifies that a data field value in ASP.NET Dynamic Data must match the specified regular expression.

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 資料欄位。The following example shows how to use the RegularExpressionAttribute attribute to validate the FirstName and LastName data fields. 正則運算式允許最多40的大寫和小寫字元。The regular expression allows up to 40 uppercase and lowercase characters. 此範例會執行下列工作:The example performs the following tasks:

  • 會執行中繼資料部分類別和相關聯的中繼資料類別。Implements a metadata partial class and the associated metadata class.

  • 在相關聯的中繼資料類別中,將 RegularExpressionAttribute 屬性套用至 FirstName 和 LastName 資料欄位,並指定模式和自訂錯誤訊息。In the associated metadata class, applies the RegularExpressionAttribute attribute to the FirstName and LastName data fields, specifying the pattern and custom error messages.

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 屬性套用至屬性。You apply the RegularExpressionAttribute attribute to a property when you need to validate values for the property against a regular expression. 正則運算式可讓您精確地指定有效值的格式。The regular expression enables you to specify very precisely the format of valid values. Pattern 屬性包含正則運算式。The Pattern property contains the regular expression. 如果屬性的值為 null 或空字串 ( "" ) ,此值會自動傳遞 RegularExpressionAttribute 屬性的驗證。If the value of the property is null or an empty string (""), the value automatically passes validation for the RegularExpressionAttribute attribute. 若要驗證值不是 null 或空字串,請使用 RequiredAttribute 屬性。To validate that the value is not null or an empty string, use the RequiredAttribute attribute.

正則運算式會搜尋完全相符的結果,而不是在模式結尾使用,因此會 ^ $ 產生與使用它相同的結果。The regular expression searches for an exact match, not using ^ before and $ at the end of the pattern produces the same results as using it. 若為搜尋叫用,請在前面加上並附加模式 .*For a search hit, prepend and append the pattern with .*.

建構函式

RegularExpressionAttribute(String)

初始化 RegularExpressionAttribute 類別的新執行個體。Initializes a new instance of the RegularExpressionAttribute class.

屬性

ErrorMessage

取得或設定錯誤訊息,此錯誤訊息會在驗證失敗時與驗證控制項產生關聯。Gets or sets an error message to associate with a validation control if validation fails.

(繼承來源 ValidationAttribute)
ErrorMessageResourceName

取得或設定要在驗證失敗時用來查閱 ErrorMessageResourceType 屬性值的錯誤訊息資源名稱。Gets or sets the error message resource name to use in order to look up the ErrorMessageResourceType property value if validation fails.

(繼承來源 ValidationAttribute)
ErrorMessageResourceType

取得或設定資源類型,此類型可在驗證失敗時用於查閱錯誤訊息。Gets or sets the resource type to use for error-message lookup if validation fails.

(繼承來源 ValidationAttribute)
ErrorMessageString

取得當地語系化的驗證錯誤訊息。Gets the localized validation error message.

(繼承來源 ValidationAttribute)
MatchTimeoutInMilliseconds

取得或設定執行單一比對作業,直到作業逾時之前的時間量 (以毫秒為單位)。Gets or sets the amount of time in milliseconds to execute a single matching operation before the operation times out.

Pattern

取得規則運算式模式。Gets the regular expression pattern.

RequiresValidationContext

取得值,這個值表示屬性是否需要驗證內容。Gets a value that indicates whether the attribute requires validation context.

(繼承來源 ValidationAttribute)
TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。When implemented in a derived class, gets a unique identifier for this Attribute.

(繼承來源 Attribute)

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。Returns a value that indicates whether this instance is equal to a specified object.

(繼承來源 Attribute)
FormatErrorMessage(String)

格式化要在規則運算式驗證失敗時顯示的錯誤訊息。Formats the error message to display if the regular expression validation fails.

GetHashCode()

傳回這個執行個體的雜湊碼。Returns the hash code for this instance.

(繼承來源 Attribute)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

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

檢查指定的值在目前的驗證屬性方面是否有效。Checks whether the specified value is valid with respect to the current validation attribute.

(繼承來源 ValidationAttribute)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(繼承來源 Attribute)
IsValid(Object)

檢查使用者輸入的值是否符合規則運算式模式。Checks whether the value entered by the user matches the regular expression pattern.

IsValid(Object, ValidationContext)

根據目前的驗證屬性,驗證指定的值。Validates the specified value with respect to the current validation attribute.

(繼承來源 ValidationAttribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

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

驗證指定的物件。Validates the specified object.

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

驗證指定的物件。Validates the specified object.

(繼承來源 ValidationAttribute)

明確介面實作

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。Maps a set of names to a corresponding set of dispatch identifiers.

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

擷取物件的類型資訊,可以用來取得介面的類型資訊。Retrieves the type information for an object, which can be used to get the type information for an interface.

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

擷取物件提供的類型資訊介面數目 (0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

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

提供物件所公開的屬性和方法的存取權。Provides access to properties and methods exposed by an object.

(繼承來源 Attribute)

適用於