Solved

RegEx needed to handle zip codes

Posted on 2008-10-29
2
210 Views
Last Modified: 2010-04-14
I need RegEx that will allow the following zip codes.



city            state   country ZIP
Seacliff Park   ZZ      AU      5049
Calgary         AB      CA      T2J7C9
Tokyo           ZZ      JP      1070052
Fareham         ZZ      UK      PO15 5RQ
Las Vegas       NV      US      89123-2545
0
Comment
Question by:knowlton
  • 2
2 Comments
 
LVL 13

Accepted Solution

by:
numberkruncher earned 500 total points
ID: 22836268
I think that you are going to need one regex for each country. You can store an array of regex rules and then select the one you need based upon country code. This way, you can validate a zip/postal code based upon the location. A UK postal code is invalid in the US for example.

Alternatively you could have a really long regular expression along the lines of the following, but this would not validate zip/postal codes depending upon the country.

(UK)|(AU)|(CA)...

Where UK would be interchanged with the expression below.
// Example for UK

Regex re = new Regex("[a-z][a-z][0-9][0-9]?[ \t]*[0-9][0-9]?[a-z][a-z]", RegexOptions.IgnoreCase);

Text = re.IsMatch(textBox1.Text).ToString();

Open in new window

0
 
LVL 13

Expert Comment

by:numberkruncher
ID: 22836304
In case you are unaware of what the operators I've used are:

[a-z]    - Option selector, must be one of the listed values (in this case a, b, ..., z).
?         - Indicate preceding test is optional.
*          - Indicates that there can be zero or more of previous item.
+         - Indicates that there must be at least one or more of previous item (I didn't use this one above).
|          - Alternate, either left or right test must occur, can be equivalent to option selector (a|b|c|d|...|z) or a more advanced (abc)|(def).
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

Suggested Solutions

Title # Comments Views Activity
JavaScript error 1 52
Asp.net mvc 5 5 43
Resolve Dependency Issues 4 44
.NET tools for adding thread safety to a web app? 3 13
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

911 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

25 Experts available now in Live!

Get 1:1 Help Now