Canadian postal code validation
I need to validate a Canadian postal code (for example, M4B 1C7
) using C# (.NET) regular expressions.
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]