473,399 Members | 3,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Zip code validation using regular expressions

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
2 3854
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mike | last post by:
Related to another topic I just posted, I wanted to discuss ways to optimize the validation of very large (>100MB) XML documents. First, I have no idea if something like this already exists; it...
21
by: AnnMarie | last post by:
<script language="JavaScript" type="text/javascript"> <!-- function validate(theForm) { var validity = true; // assume valid if(frmComments.name.value=='' && validity == true) { alert('Your...
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
10
by: A.M | last post by:
Hi, How can I use validation controls to check max length of string text boxes? Thanks, Alan
5
by: JIM.H. | last post by:
Hello, I have this validation expression: ^(?:(?:0?|1)|(?:0?|11)(?!\/31)|(?:0?2)(?:(?!\/3|\/29\/(?:(?:0||)00|(?:\d{2}(?:0||))))))\/(?:0?||3)\/\d{4}$ This is supposed to match MM/DD/YYYY it is...
35
by: Mika M | last post by:
Simple question: Does Framework (1.1) contain any routine to check entered email-address is valid ? It's quite easy to make own code for that purpose, but why to do if Framework (1.1) contain...
4
by: Együd Csaba | last post by:
Hi All, I'd like to "compress" the following two filter expressions into one - assuming that it makes sense regarding query execution performance. .... where (adate LIKE "2004.01.10 __:30" or...
17
by: Mark | last post by:
I must create a routine that finds tokens in small, arbitrary VB code snippets. For example, it might have to find all occurrences of {Formula} I was thinking that using regular expressions...
2
by: jack | last post by:
Im a new bee in validation Im just in the design phase of the project and not thinking about having a saperate validaion block. Just browsed through validaion block of enterprise library which...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.