Zip code validation using regular expressions

A

Alpha83

Hi,
Is it safe or error-free to validate country zip-codes using regular
expressions - especially for countries like UK or Canada, where there
are different alphanumeric formats. Here's what I got for UK -
System.Text.RegularExpressions.Regex(@"^(([A-Z]{1,2}[0-9][0-9A-Z]{0,1})
\ ([0-9][A-Z]{2}))|(GIR\ 0AA)$").

Got this from regex.lib, and needed to know also if there are better
approaches for zip code validation.

Thanks.
 
K

KH

Regular expressions will work fine for something like that; my only comment
is that they can sometimes be like using a sledgehamed to drive a finishing
nail... they are sometimes (dare I say often?) overkill for what someone is
trying to accomplish.

On the other hand you can store regex patterns in resource files so they can
be changed w/o recompiling the app which could be very handy.

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top