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]{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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s