By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,671 Members | 1,387 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,671 IT Pros & Developers. It's quick & easy.

Zip code validation using regular expressions

P: n/a
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.
Jul 15 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
"Alpha83" wrote:
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.
Jul 15 '08 #2

P: n/a
"Alpha83" <ta*****@gmail.comwrote:
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.
I think that regular expression is about as good as you'll get for the UK,
but it's still not perfect because it permits some postcodes that don't
exist. (For example, just because there's N for North London and it's
followed by a number, there doesn't have to be an N99 region defined yet.)
Also, you need to be willing to keep it up to date, because postcodes can
change and evolve over time with building and population increases, etc.

Eq.
Jul 15 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.