Created
January 21, 2015 01:43
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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