I need to validate a Canadian postal code (for example, M4B 1C7
) using C# (.NET) regular expressions.
The postal code is a six-character uniformly structured, alphanumeric code in the form “ANA NAN” where “A” is an alphabetic character and “N” is a numeric character. Two segments make up a postal code: Forward Sortation Area (FSA) and Local Delivery Unit (LDU).
You will see this error when the zip or postal code of your address does not match your purchasing card. Simply review what the postal or zip code of your purchasing card is, and re-enter the correct information to proceed.
Canadian postal codes can't contain the letters D, F, I, O, Q, or U, and cannot start with W or Z:
[ABCEGHJKLMNPRSTVXY][0-9][ABCEGHJKLMNPRSTVWXYZ][0-9][ABCEGHJKLMNPRSTVWXYZ][0-9]
If you want an optional space in the middle:
[ABCEGHJKLMNPRSTVXY][0-9][ABCEGHJKLMNPRSTVWXYZ] ?[0-9][ABCEGHJKLMNPRSTVWXYZ][0-9]
Here are the rules http://en.wikipedia.org/wiki/Postal_code#Reserved_characters
ABCEGHJKLMNPRSTVXY <-- letter used
DFIOQU <-- letters not used because it mixes up the reader
WZ <-- letters used but not in the first letter
With that in mind the following in the proper regex
@[ABCEGHJKLMNPRSTVXY][0-9][ABCEGHJKLMNPRSTVWXYZ][\s][0-9][ABCEGHJKLMNPRSTVWXYZ][0-9]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With