Solved

Vaildate Zip Code Using a Regular Expression

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

19 Experts available now in Live!

Get 1:1 Help Now