#12146 closed (fixed)
Canada localflavor: postal code validation too lax
Reported by: | Paul Schreiber | Owned by: | Karen Tracey |
---|---|---|---|
Component: | contrib.localflavor | Version: | 1.1 |
Severity: | Keywords: | canada postal code | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
In the Canada localflavor package, postal code validation is too lax. As of Django 1.1.1, we have this:
def __init__(self, *args, **kwargs): super(CAPostalCodeField, self).__init__(r'^[ABCEGHJKLMNPRSTVXYZ]\d[A-Z] \d[A-Z]\d$',
However, no postal code includes the letters D, F, I, O, Q, or U.
The letters W and Z are used, but are not currently used as the first letter.
http://en.wikipedia.org/wiki/Canadian_postal_code#Number_of_possible_postal_codes
I can't find this on the Canada Post web site, but it's documented in many places.
You want something like this:
def __init__(self, *args, **kwargs): super(CAPostalCodeField, self).__init__(r'^[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ] \d[ABCEGHJKLMNPRSTVWXYZ]\d$',
Attachments (2)
Change History (8)
comment:1 by , 15 years ago
comment:2 by , 15 years ago
I was able to find the meaning of the first code in the FSA here:
http://www.columbia.edu/kermit/postal-ca.html
To quote:
The first letter of the Forward Sortation Area (FSA) uniquely identifies the province or territory, except in a few cases to be explained later. Each province or territory has one or more identifying letters, as follows: A Newfoundland and Labrador B Nova Scotia C Prince Edward Island E New Brunswick G Québec East H Montréal Metropolitan J Québec West K Eastern Ontario L Central Ontario M Toronto N Southwestern Ontario P Northern Ontario R Manitoba S Saskatchewan T Alberta V British Columbia X Northwest Territories and Nunavut Y Yukon Territory
it continues later
Note that in addition to avoiding the six "forbidden" letters above, this chart implies that W and Z also do not appear as the first letter of a CPC (at least not at present).
This list can be validated against the Canadian Post website map. http://www.canadapost.ca/tools/pg/manual/PGaddress-e.asp#1402170
comment:3 by , 15 years ago
Has patch: | set |
---|---|
Owner: | changed from | to
comment:4 by , 15 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
I'm having some trouble with the wording of the wikipedia article. "The letters W and Z are used, but are not currently used as the first letter." Does this mean the first letter of the Postal Code, (Zxx xxx) or does it apply to the first letter in both the FSA and the LDU (Zxx Zxx)?
I was unable to find any relevant information after a quick search. I would like to see another source validating this besides wikipedia.