Solved

RegEx needed to handle zip codes

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

735 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