4/21/2009

Handy Regular Expressions(RegEx) - Zip/Postal code validation for US and Canada

Zip code validation for US and Canada is most common validation required for any web site where you have address input. Here is the handy regular expressions, which can be used for Zip/Postal code validation on your web page.

RegEx,zip code regular expression, postal code regular expression, us zip code validation, us postal code validation, Canada zip code validation, Canada postal code validation,regex zip code, regex postal code,US and Canada zip code validation, US and Canada postal code validation, US and Canada zip code regex, US and Canada zip code regular expression

  1. Separate address forms for US/Canada


  2. If you have separate address entry forms for US and Canada then you can use following regular expressions to validate the zip/postal code in your forms.

    For US Zip code validation Use below regular Expression


    ^([0-9]{5})$



    For Canada Postal code validation Use below regular Expression to validate the zip/postal code in your form.


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


  3. Single Address entry form for US and Canada


  4. If you have a common form for US and Canada Address entry then use below Regular expression, which is going to allow US Zip code and Canada Postal Code in same field.


    For US zip code & Canada Postal code validation both in same field use below regular Expression



    (^([0-9]{5})$)|(^[ABCEGHJKLMNPRSTVXYabceghjklmnprstvxy]{1}\d{1}[A-Za-z]{1} *\d{1}[A-Za-z]{1}\d{1}$)



    In case you have not observed this, the regular expression for validating both Zip code and Postal code is a OR condition expression combining the two separate regular expressions for US Zip code and Canada Postal code with a pipe (|) separator.

  5. The nine digit zip code for US


There are times when you may want to use the 9 digit zip code validation for US.
The 9 digit zip code is usually in format like this #####-####.

For a 9 digit US zip code validation use below regular Expression



^([0-9]{5}-[0-9]{4})$




  1. Separate address forms for US/Canada

  2. Single Address entry form for US and Canada

  3. The nine digit zip code for US



regular expression Canada postal code, regular expression validate canada postal code,regular expression US postal code,regular expression canada zip code,regular expression US Zip code
Validate Zip Postal Code using regular expressions, block invalid zip code with regular expression. USA zip code regex, United States zip postal code regex

10 comments:

  1. There can be variations in different country zip codes but the key is understand how you can combine all this in same place.

    ReplyDelete
  2. Ahem, in the USA, we have 9 digit zips...

    ^([0-9]{5}(-[0-9]{4})?)$

    (untested by I think it's right.)

    ReplyDelete
  3. @James Thanks for your comment. This type of zip code is probably related to the PO boxes. But certainly needs to be validated at most of the places. I will update my post with your expression.

    ReplyDelete
  4. All USA addresses have a 9 digit zip code not just PO boxes...
    http://en.wikipedia.org/wiki/ZIP_code#ZIP_.2B_4

    ReplyDelete
  5. that may be, but everything i've ever sent to the US, i've only ever used the 5-digit version.

    ReplyDelete
  6. @James Curran Thanks for your comment I have updated this post with your regex too. Let me know any other feedback.

    ReplyDelete
  7. Nice information, I really appreciate the way you presented.Thanks for sharing..

    http://www.w3cvalidation.net/

    ReplyDelete
  8. Very Helpful Reg expressions.
    I used these for validating the big project site at

    Best Usa Classifieds


    Thx a millions.

    ReplyDelete
  9. These allow a person to enter all zeroes for a US Zipcode. Not allowed.

    ReplyDelete
  10. i want to use one regularexpession to validate any country zipcode.please help me

    ReplyDelete

Got something to say? Don't hold it! Tell it to us.

You Might Like

.....