Opened 5 years ago

Closed 5 years ago

Last modified 4 years ago

#12146 closed (fixed)

Canada localflavor: postal code validation too lax

Reported by: paulschreiber Owned by: kmtracey
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: UI/UX:

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)

ca_localflavor.diff (623 bytes) - added by mlavin 5 years ago.
Patch for new regular expression
ca_localflavor_with_tests.diff (2.8 KB) - added by mlavin 5 years ago.
New patch with tests.

Download all attachments as: .zip

Change History (8)

comment:1 Changed 5 years ago by agabel

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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.

comment:2 Changed 5 years ago by mlavin

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

Changed 5 years ago by mlavin

Patch for new regular expression

comment:3 Changed 5 years ago by mlavin

  • Has patch set
  • Owner changed from nobody to kmtracey

Changed 5 years ago by mlavin

New patch with tests.

comment:4 Changed 5 years ago by kmtracey

  • Resolution set to fixed
  • Status changed from new to closed

(In [11844]) Fixed #12146: Corrected Canadian postal code validation. Thanks paulschreiber and Mark Lavin.

comment:5 Changed 5 years ago by kmtracey

(In [11845]) [1.1.X] Fixed #12146: Corrected Canadian postal code validation. Thanks paulschreiber and Mark Lavin.

r11844 from trunk.

comment:6 Changed 4 years ago by jacob

  • milestone 1.2 deleted

Milestone 1.2 deleted

Note: See TracTickets for help on using tickets.
Back to Top