The following example shows how to use the RegularExpressionAttribute attribute to validate the FirstName and LastName data fields. 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.
In the associated metadata class, applies the RegularExpressionAttribute attribute to the FirstName and LastName data fields, specifying the pattern and custom error messages.
Imports System
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
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;
}