Geeks With Blogs

News
 
 
My Visual Identity
 
www.flickr.com
This is a Flickr badge showing photos in a set called Personal Favorites. Make your own badge here.

 

maina donaldson a pragmatist's blog
I had to spent more time than I wished I would spend last weekend trying to find a RegEx that would validate US and Canadian postal codes for me.   So here for the record, are the regular expression strings that I was looking for

^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$
Matches Canadian PostalCode formats with or without spaces (e.g., "T2X 1V4" or "T2X1V4")

^\d{5}(-\d{4})?$
Matches all US format zip code formats (e.g., "94105-0011" or "94105")

(^\d{5}(-\d{4})?$)|(^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$)
Matches US or Canadian zip codes in above formats.
 

Posted on Monday, December 3, 2007 11:19 AM | Back to top


Comments on this post: US and Canada zip code validation RegEx

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
Sweet, thanks Maina, that'll go into the library.
Left by Aaron on Dec 03, 2007 11:48 AM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
Hi,

I have tried with above three expressions. Last expression i.e.Matches US or Canadian zip codes in above formats is not validating if I enter 12345 e123 or 12345 123r.

Thanks,
Prashant
Left by Prashant Kulkarni on Feb 22, 2008 4:01 AM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
'XUIIX2A 3M5BBBBBB' also goes like a valid one........
Left by sharmutta on May 22, 2008 2:31 PM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
In your combined US/Canada pattern, you probably wanna enclose the 2 parts in parens. And what's up with ? at the end?
Left by Timothy Jones on May 26, 2008 12:20 PM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
Thank you all for your feedback. I've edited the RE and I believe it addressed all issues mentioned.
Left by maina on May 27, 2008 10:56 AM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
Thanks, it really worked
Left by Sudhir on Aug 21, 2008 6:21 AM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
Thanks for the code, Maina! Works perfect. Here is small alternation for people who put small letters in Canadian Postal Code:

^[ABCEGHJKLMNPRSTVXYabceghjklmnprstvxy]{1}\d{1}[A-Za-z]{1} *\d{1}[A-Za-z]{1}\d{1}$

By the way, if anyone wants to find out RegEx for UK Postal Code, here is a quick fix: http://snipplr.com/view/7990/validate-uk-postcode/
Left by Tony on Sep 11, 2008 8:06 AM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
So, how's this:

(^\d{5}(-\d{4})?$)|(^[ABCEGHJKLMNPRSTVXYabceghjklmnprstvxy]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$)

Does this allow for uppercase and lowercase on the Canadian postal codes? I'll test it and let you know.
Left by Bryan on Oct 28, 2008 1:32 PM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
Technicaly, the letters D, F, I, O, Q and U are never used in a Canadian postal code. And the letters W and Y are never at first position. Here is the regexp : /[abceghjklmnprstvxz]\d[abceghjklmnprstvwxyz]\d[ ]*\d[abceghjklmnprstvwxyz]\d/i
Left by carpi on Nov 11, 2008 7:55 AM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
I put the code and one of the user complained that it wont work, The user's canada Zip code is M5R 1GR that means the pattern is changed. Now in this Zip code its ANA NAA. So how to change it in the expression.
Left by Vishi on Nov 18, 2008 3:59 PM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
I tried to solve the problem and I succeeded in that. (^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}[A-Z,1-9]{1}$)
Left by Vishi on Nov 19, 2008 3:12 AM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
Try this..
<asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server" ControlToValidate="txtZip" Display="Dynamic" ErrorMessage="Invalid zip" ValidationExpression="\d{5}((-)?\d{4})?|([A-Za-z]\d[A-Za-z]( )?\d[A-Za-z]\d)">
</asp:RegularExpressionValidator>
Left by Sanoop,Forent Infosolutions Ltd on Dec 03, 2008 1:37 PM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
i cannot add my employers postal code in my taxes it will not take it it says it is invalid because of the numbers.
Left by susan bryce on Jan 24, 2009 3:06 PM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
Thanks! Just did a google search for zipcode validation and this right up. Thanks for sharing and saving us time!
Left by paul on Feb 08, 2009 8:08 PM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
Thanks.Its works great and helpful.
Left by Megha on May 21, 2009 2:16 PM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
I've been searching for a good regex that uses word boundries and not start and end of line metacharacters. This doesn't work if the zip is not the only item on the line.
Left by JT on May 23, 2009 7:16 AM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
Thanks this was really helpful.
Left by Priya on Jun 26, 2009 10:33 AM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
Nice one. It solves the purpose.
Left by Pompana Gouda on Jun 30, 2009 5:28 AM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
Actually, according to the USPS Post office, there can be less than 5 digits in the main part of the zip code so '\d{5}' will fail. Apparently they can go from 3 to 5 so you'd have to start your regex with '\d{3,5} instead I think.
Left by Nonymous on Aug 19, 2009 7:28 PM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
Someone once said of one of the original unix programmers "He thought deeply about simple things". This is a great little piece of regex that echos the above. It makes me happy. Thanks!
Left by Frank on Jul 19, 2010 1:21 AM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
I m really thankful it saved my time
and working fine
Left by Khurram Shahzad on Sep 29, 2010 4:50 AM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
Million Thanks.

I am a lead programmmer for

Best Usa Classifieds


I have used the Regular expressions for the best classified portal.

Great help for us.
Left by JOANNA on Oct 08, 2010 6:20 PM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
Another site using RegEx is openlist.org

Thanks
Left by Ross J. on Dec 01, 2010 9:34 PM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
Hey your regexp is pretty good for Canadian zipcode but i just a question since you are saying Canadian Zip code can't contain certain letters such as: "D,F,I,O,Q or U". your expression is gives only 89% correct. now if you copy and paste or maybe just take these letters out of the 3rd and 5th char. It'll give you 100% result.

I'm not sure if I just made my self look stupid but that's what I came up with.

Thanks,
Left by TN Jr. on Jan 29, 2011 10:19 AM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
Great post. Thank you for posting it.
Left by Sandy on Apr 07, 2011 5:25 AM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
Thank you. Exactly what I was looking for
Left by jerry on May 11, 2011 6:31 PM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
this really works for jsp
Left by Apoorva on Jun 22, 2011 12:56 AM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
Thanks, very helpful
Left by Kevin on Jul 03, 2011 8:43 PM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
Thanks for sharing, works great!
Left by Ann on Jul 19, 2011 4:18 PM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
This regex will validate Canada Zip Code.

String canZipRegEx1 = "^([0-9A-Za-z]{3}[\\s-][0-9A-Za-z]{3})$";
String canZipRegEx2 = "^([0-9A-Za-z]{6})$";

if ( (postalCode.trim().matches(canZipRegEx1)) || (postalCode.trim().matches(canZipRegEx2)) )
{
return true;
}
Left by mrpr on Aug 02, 2011 11:00 AM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
for completeness I'd add lowercase letters. ^[abceghjklmnprstvxyABCEGHJKLMNPRSTVXY]{1}\d{1}[a-zA-Z]{1} *\d{1}[a-zA-Z]{1}\d{1}$
Left by limeyd on Aug 25, 2011 3:22 PM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
You are a GOD amongst men..lol

Thanks!
Left by EJ on Sep 25, 2011 1:47 PM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
You just saved me a lot of time. Thank you.
Left by Chris Sobolewski on Dec 01, 2011 9:34 AM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
Great job!

Thank you!!
Left by Leon on Dec 07, 2011 8:31 AM

# US and Canada zip code validation RegEx
Requesting Gravatar...
Great job

Thank you
Left by Leon on Dec 07, 2011 8:32 AM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
Finally one that WORKS !
thx!
Left by Randy on Dec 07, 2011 1:13 PM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
I have a problem about your regular expression....suppose i select a Canada as a country & write the postal code in US format then also it works and don't show any error message...how it ??????
Left by chets on Feb 09, 2012 1:23 PM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
This is great to validate the Canadian Postal Code. But is there a regex to format it correctly? If I have l8n1n1 I would like it corrected to L8N 1N1 - is this possible?
Left by David Anderson on Mar 14, 2012 1:55 AM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
Thanks a lot
Left by Rakeh on Jun 05, 2012 2:37 AM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
Thanks, it works....
Left by Dipesh Malani on Sep 10, 2012 2:40 AM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
I need one more info here , suppose users are searching records depending on canada zipcode, and they enter A2S instead of "A2s 3F4" or "A2s3F4", then how the regular expression look like.

I tried many times but no luck.

Hoping for your response.
Left by Preeti Deshpande on Nov 29, 2012 6:39 AM

# re: US and Canada zip code validation RegEx
Requesting Gravatar...
This might be a bit better this allows the zip to be entered if the user didn't enter a - in between. ^\d{5}(-?\d{4})?$
Left by mvmrocks on Dec 01, 2012 11:30 PM

Your comment:
 (will show your gravatar)


Copyright © Maina Donaldson | Powered by: GeeksWithBlogs.net | Join free