Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.1.X] Fixed #12146: Corrected Canadian postal code validation. Than…

…ks paulschreiber and Mark Lavin.

r11844 from trunk.


git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@11845 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 64736fe37f366519b52d236d1432c7b1fd831b02 1 parent b5ffd10
Karen Tracey authored December 13, 2009
11  django/contrib/localflavor/ca/forms.py
@@ -12,13 +12,20 @@
12 12
 sin_re = re.compile(r"^(\d{3})-(\d{3})-(\d{3})$")
13 13
 
14 14
 class CAPostalCodeField(RegexField):
15  
-    """Canadian postal code field."""
  15
+    """
  16
+    Canadian postal code field.
  17
+
  18
+    Validates against known invalid characters: D, F, I, O, Q, U
  19
+    Additionally the first character cannot be Z or W.
  20
+    For more info see:
  21
+    http://www.canadapost.ca/tools/pg/manual/PGaddress-e.asp#1402170
  22
+    """
16 23
     default_error_messages = {
17 24
         'invalid': _(u'Enter a postal code in the format XXX XXX.'),
18 25
     }
19 26
 
20 27
     def __init__(self, *args, **kwargs):
21  
-        super(CAPostalCodeField, self).__init__(r'^[ABCEGHJKLMNPRSTVXYZ]\d[A-Z] \d[A-Z]\d$',
  28
+        super(CAPostalCodeField, self).__init__(r'^[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ] \d[ABCEGHJKLMNPRSTVWXYZ]\d$',
22 29
             max_length=None, min_length=None, *args, **kwargs)
23 30
 
24 31
 class CAPhoneNumberField(Field):
44  tests/regressiontests/forms/localflavor/ca.py
@@ -60,6 +60,50 @@
60 60
 u''
61 61
 >>> f.clean('')
62 62
 u''
  63
+>>> f.clean('W2S 2H3')
  64
+Traceback (most recent call last):
  65
+...
  66
+ValidationError: [u'Enter a postal code in the format XXX XXX.']
  67
+>>> f.clean('T2W 2H7')
  68
+u'T2W 2H7'
  69
+>>> f.clean('T2S 2W7')
  70
+u'T2S 2W7'
  71
+>>> f.clean('Z2S 2H3')
  72
+Traceback (most recent call last):
  73
+...
  74
+ValidationError: [u'Enter a postal code in the format XXX XXX.']
  75
+>>> f.clean('T2Z 2H7')
  76
+u'T2Z 2H7'
  77
+>>> f.clean('T2S 2Z7')
  78
+u'T2S 2Z7'
  79
+>>> f.clean('F2S 2H3')
  80
+Traceback (most recent call last):
  81
+...
  82
+ValidationError: [u'Enter a postal code in the format XXX XXX.']
  83
+>>> f.clean('A2S 2D3')
  84
+Traceback (most recent call last):
  85
+...
  86
+ValidationError: [u'Enter a postal code in the format XXX XXX.']
  87
+>>> f.clean('A2I 2R3')
  88
+Traceback (most recent call last):
  89
+...
  90
+ValidationError: [u'Enter a postal code in the format XXX XXX.']
  91
+>>> f.clean('A2I 2R3')
  92
+Traceback (most recent call last):
  93
+...
  94
+ValidationError: [u'Enter a postal code in the format XXX XXX.']
  95
+>>> f.clean('A2Q 2R3')
  96
+Traceback (most recent call last):
  97
+...
  98
+ValidationError: [u'Enter a postal code in the format XXX XXX.']
  99
+>>> f.clean('U2B 2R3')
  100
+Traceback (most recent call last):
  101
+...
  102
+ValidationError: [u'Enter a postal code in the format XXX XXX.']
  103
+>>> f.clean('O2B 2R3')
  104
+Traceback (most recent call last):
  105
+...
  106
+ValidationError: [u'Enter a postal code in the format XXX XXX.']
63 107
 
64 108
 # CAPhoneNumberField ##########################################################
65 109
 

0 notes on commit 64736fe

Please sign in to comment.
Something went wrong with that request. Please try again.