Click here to Skip to main content
12,559,868 members (48,290 online)
Click here to Skip to main content
Add your own
alternative version


9 bookmarked

Custom Validation Control for Zip,Phone and Email

, 17 Apr 2007
Rate this:
Please Sign up or sign in to vote.
This article shows how to develop a custom validation control to validate zip,phone,email in one control.


This article provides the readers code on how to create a custom validator control that does zip,phone and email validation all in one, by specifying a property. Thought of sharing it with other code project readers to get feedback and also provide users with a ready control.


While working on a project for one of my clients, I had to create a standard form with zip,phone, email and other contact information. I had to validate the zip,email and phone for blanks and regular expressions. I started out by creating 2 controls a required field validator and regular expression validator, though the validation worked fine, the rendering was bad especially when checking for the regular expression it would show a blank and then the message, the blank being the reqd field validator.

The client was against my using javascript and so inorder to remove the gap and have only one validator control that does both i created a custom validator control and that got me what i wanted.

Using the code

In order to create a custom control, you would start with creating a class file, in this example it would be CustomZipValidator.vb and place it in the app code folder. The code in the class file is shown below, this class would inherit from the BaseValidator class and would override the EvaluateIsValid() method.

'Inherits the base validator
Public Class CustomZipPhoneEmailValidator

Inherits BaseValidator

'Override the EvaluateIsValid
Protected Overrides Function EvaluateIsValid() As Boolean

Dim value As String = Me.GetControlValidationValue(Me.ControlToValidate)

If (value.Equals(String.Empty) Or value.Length = 0) Then

Return False


If _propertyToValidate.Equals("Zip") Then

If Regex.IsMatch(value, "(^(?!0{5})(\d{5})(?!-?0{4})(-?\d{4})?$)") = False Then

ErrorMessage = "Invalid Postal Code format,Please try xxxxx or xxxxx-xxxx"

Return False


Return True

End If

End If

If _propertyToValidate.Equals("Phone") Then

If Regex.IsMatch(value, "((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}") = False Then

ErrorMessage = "Invalid Phone format,Please try (xxx)xxx-xxxx or xxx-xxx-xx"

Return False


Return True

End If

End If

If _propertyToValidate.Equals("Email") Then

If Regex.IsMatch(value, "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*") = False Then

ErrorMessage = "Not a valid email"

Return False


Return True

End If

End If

End If

End Function

Points of Interest

Coming from a background of backend business layer programming, the custom validators of ASP.NET2.0 saved my day.



This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Vinay Yeluri
Web Developer
United States United States
Sr. .NET Developer
MS in ComputerScience
Old Dominion University

You may also be interested in...


Comments and Discussions

GeneralCustom Validation Expression for Multiple email addresses (Comma or semicolon seperated) in single text box. Pin
Prabhjeet Singh20-Nov-08 19:36
memberPrabhjeet Singh20-Nov-08 19:36 
GeneralRegularExpressionValidator Pin
Member 3180654-Mar-08 1:28
memberMember 3180654-Mar-08 1:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161026.1 | Last Updated 17 Apr 2007
Article Copyright 2007 by Vinay Yeluri
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid