Opened 15 years ago

Closed 15 years ago

Last modified 13 years ago

#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)

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

Download all attachments as: .zip

Change History (8)

comment:1 by Austin Gabel, 15 years ago

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 by Mark Lavin, 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

by Mark Lavin, 15 years ago

Attachment: ca_localflavor.diff added

Patch for new regular expression

comment:3 by Mark Lavin, 15 years ago

Has patch: set
Owner: changed from nobody to Karen Tracey

by Mark Lavin, 15 years ago

New patch with tests.

comment:4 by Karen Tracey, 15 years ago

Resolution: fixed
Status: newclosed

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

comment:5 by Karen Tracey, 15 years ago

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

r11844 from trunk.

comment:6 by Jacob, 13 years ago

milestone: 1.2

Milestone 1.2 deleted

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