Validating Canadian Postal Codes

Q

How to write a regular expression to validate Canadian Postal Codes?

For example, T2X 1V4 is a valid Canadian postal codes.

✍: Guest

A

Here is the regular expression to validate Canadian postal codes:

[ABCEGHJKLMNPRSTVXY]\d[A-Z] ?\d[A-Z]\d

[ABCEGHJKLMNPRSTVXY]   - 18 FSA possible letters
\d                     - A digit
[A-Z]                  - Any upper case letter
 ?                     - An optional space
\d                     - A digit
[A-Z]                  - Any upper case letter
\d                     - A digit

Click the button to test this regular expression here online:

2013-01-29, 1994👍, 0💬