Skip to content

Instantly share code, notes, and snippets.

@KyleMit
Created January 21, 2015 01:43
Show Gist options
  • Save KyleMit/974be6525603e82c2dd9 to your computer and use it in GitHub Desktop.
Save KyleMit/974be6525603e82c2dd9 to your computer and use it in GitHub Desktop.
Validation Attribute for phone numbers that will run on the server and client
<AttributeUsage(AttributeTargets.Field Or AttributeTargets.Property, AllowMultiple:=False)>
Public Class PhoneAttribute : Inherits RegularExpressionAttribute
Private Const _pattern As String = "^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$"
Private Const _msg As String = "Format for {0} must be (888)-888-8888."
Public Sub New()
MyBase.New(_pattern)
Me.ErrorMessage = _msg
DataAnnotationsModelValidatorProvider.RegisterAdapter(
GetType(PhoneAttribute), GetType(RegularExpressionAttributeAdapter))
End Sub
End Class
'use like this
Public Class Person
<Phone()>
Public Property PhoneNumber As String
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment