- Separate address forms for US/Canada
- Single Address entry form for US and Canada
- The nine digit zip code for US
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}$
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.
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})$
- Separate address forms for US/Canada
- Single Address entry form for US and Canada
- The nine digit zip code for US
There can be variations in different country zip codes but the key is understand how you can combine all this in same place.
ReplyDeleteAhem, in the USA, we have 9 digit zips...
ReplyDelete^([0-9]{5}(-[0-9]{4})?)$
(untested by I think it's right.)
@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.
ReplyDeleteAll USA addresses have a 9 digit zip code not just PO boxes...
ReplyDeletehttp://en.wikipedia.org/wiki/ZIP_code#ZIP_.2B_4
that may be, but everything i've ever sent to the US, i've only ever used the 5-digit version.
ReplyDelete@James Curran Thanks for your comment I have updated this post with your regex too. Let me know any other feedback.
ReplyDeleteNice information, I really appreciate the way you presented.Thanks for sharing..
ReplyDeletehttp://www.w3cvalidation.net/
Very Helpful Reg expressions.
ReplyDeleteI used these for validating the big project site at
Best Usa Classifieds
Thx a millions.
These allow a person to enter all zeroes for a US Zipcode. Not allowed.
ReplyDeletei want to use one regularexpession to validate any country zipcode.please help me
ReplyDelete