Solved

Vaildate Zip Code Using a Regular Expression

Posted on 2004-10-10
3
237 Views
Last Modified: 2010-04-23
Hello, I would like to validate a zip code using a regular expression.  The zip code must be in one of two formats.

The first is:

nnnnn where n is any digit including zero.  5 digits are required (they must all be numbers)

The second is:

nnnnn-nnnn where n is any digit including zero.  this MUST have the hypen between the fifth and seventh character and there must be 5 numercial values to the left of the hypen and four numerical values to the right of the hypen.  

Thanks for your help, sorry I'm bad at these :-)
0
Comment
Question by:dsulli2000
  • 2
3 Comments
 
LVL 10

Expert Comment

by:Hans Langer
ID: 12273649
Hi!, Try this,

Digit Zipcode
^\d{5}$ matches 12879, 97211.

^\d{5}-\d{4}$ matches 97211-1234.

GL
0
 
LVL 2

Expert Comment

by:GohdanTheMoblin
ID: 12277782
You can catch either one with only one expression:
^\d{5}(-\d{4})?$
0
 
LVL 2

Accepted Solution

by:
GohdanTheMoblin earned 125 total points
ID: 12277817
Although now that I think about it, you may mean that you aren't sure how to write a function to do it, so I figured while I'm here I'd make it:

    Imports System.Text.RegularExpressions 'add this to the file

    Private Function IsValidZip(ByVal ZipString As String) As Boolean
        Dim pattern As String = "^\d{5}(-\d{4})?$"
        Dim expression As Regex = New Regex(pattern)

        Return expression.IsMatch(ZipString)
    End Function

This function will return true if the argument string is a valid Zip Code, 5-digit or 9-digit.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now