[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Vaildate Zip Code Using a Regular Expression

Posted on 2004-10-10
3
Medium Priority
?
248 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

656 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