Alex Rivera | Logout

Canadian postal code validation

Asked 2009-07-17T23:54:14.637
64

I need to validate a Canadian postal code (for example, M4B 1C7) using C# (.NET) regular expressions.

Edit
Report

1 Answer

109

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]
answered 2009-07-18T00:08:31.377

Your Answer