Solved

Vaildate Zip Code Using a Regular Expression

Posted on 2004-10-10
3
241 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 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