?
Solved

Vaildate Zip Code Using a Regular Expression

Posted on 2004-10-10
3
Medium Priority
?
243 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

770 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