Solved

RegEx needed to handle zip codes

Posted on 2008-10-29
2
209 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 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

17 Experts available now in Live!

Get 1:1 Help Now