Search
Close this search box.

US and Canada zip code validation RegEx

 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]\d[A-Z] *\d[A-Z]\d
Matches Canadian PostalCode formats with or without spaces (e.g., "T2X 1V4" or "T2X1V4")
[0-9]{5}(-[0-9]{4})?
Matches all US format zip code formats (e.g., "94105-0011" or "94105")
[0-9]{5}(-[0-9]{4})?|[ABCEGHJKLMNPRSTVXY]\d[A-Z] *\d[A-Z]\d?
Matches US or Canadian zip codes in above formats.
This article is part of the GWB Archives. Original Author: maina Donaldson

Related Posts