?
Solved

RegEx needed to handle zip codes

Posted on 2008-10-29
2
Medium Priority
?
216 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:Tom Knowlton
[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
2 Comments
 
LVL 13

Accepted Solution

by:
numberkruncher earned 2000 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

Independent Software Vendors: 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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

765 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