When dealing with magento or any other ecommerce websites which is meant to be targeted for specific countries or not we must check validity of the postal code
On one of my recent projects I come with the same problem here I need to validate the postal code for US and Canada
and I found some code snippets to to do so and sharing it on the title US and Canada postal code validation RegEx

^[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.

Hop this will help someone for US and Canada postal code validation RegEx

Leave a Reply

Your email address will not be published.