Author: kmtracey Date: 2009-12-13 11:01:22 -0600 (Sun, 13 Dec 2009) New Revision: 11844
Modified: django/trunk/django/contrib/localflavor/ca/forms.py django/trunk/tests/regressiontests/forms/localflavor/ca.py Log: Fixed #12146: Corrected Canadian postal code validation. Thanks paulschreiber and Mark Lavin. Modified: django/trunk/django/contrib/localflavor/ca/forms.py =================================================================== --- django/trunk/django/contrib/localflavor/ca/forms.py 2009-12-13 16:24:36 UTC (rev 11843) +++ django/trunk/django/contrib/localflavor/ca/forms.py 2009-12-13 17:01:22 UTC (rev 11844) @@ -12,13 +12,20 @@ sin_re = re.compile(r"^(\d{3})-(\d{3})-(\d{3})$") class CAPostalCodeField(RegexField): - """Canadian postal code field.""" + """ + Canadian postal code field. + + Validates against known invalid characters: D, F, I, O, Q, U + Additionally the first character cannot be Z or W. + For more info see: + http://www.canadapost.ca/tools/pg/manual/PGaddress-e.asp#1402170 + """ default_error_messages = { 'invalid': _(u'Enter a postal code in the format XXX XXX.'), } def __init__(self, *args, **kwargs): - super(CAPostalCodeField, self).__init__(r'^[ABCEGHJKLMNPRSTVXYZ]\d[A-Z] \d[A-Z]\d$', + super(CAPostalCodeField, self).__init__(r'^[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ] \d[ABCEGHJKLMNPRSTVWXYZ]\d$', max_length=None, min_length=None, *args, **kwargs) class CAPhoneNumberField(Field): Modified: django/trunk/tests/regressiontests/forms/localflavor/ca.py =================================================================== --- django/trunk/tests/regressiontests/forms/localflavor/ca.py 2009-12-13 16:24:36 UTC (rev 11843) +++ django/trunk/tests/regressiontests/forms/localflavor/ca.py 2009-12-13 17:01:22 UTC (rev 11844) @@ -60,6 +60,50 @@ u'' >>> f.clean('') u'' +>>> f.clean('W2S 2H3') +Traceback (most recent call last): +... +ValidationError: [u'Enter a postal code in the format XXX XXX.'] +>>> f.clean('T2W 2H7') +u'T2W 2H7' +>>> f.clean('T2S 2W7') +u'T2S 2W7' +>>> f.clean('Z2S 2H3') +Traceback (most recent call last): +... +ValidationError: [u'Enter a postal code in the format XXX XXX.'] +>>> f.clean('T2Z 2H7') +u'T2Z 2H7' +>>> f.clean('T2S 2Z7') +u'T2S 2Z7' +>>> f.clean('F2S 2H3') +Traceback (most recent call last): +... +ValidationError: [u'Enter a postal code in the format XXX XXX.'] +>>> f.clean('A2S 2D3') +Traceback (most recent call last): +... +ValidationError: [u'Enter a postal code in the format XXX XXX.'] +>>> f.clean('A2I 2R3') +Traceback (most recent call last): +... +ValidationError: [u'Enter a postal code in the format XXX XXX.'] +>>> f.clean('A2I 2R3') +Traceback (most recent call last): +... +ValidationError: [u'Enter a postal code in the format XXX XXX.'] +>>> f.clean('A2Q 2R3') +Traceback (most recent call last): +... +ValidationError: [u'Enter a postal code in the format XXX XXX.'] +>>> f.clean('U2B 2R3') +Traceback (most recent call last): +... +ValidationError: [u'Enter a postal code in the format XXX XXX.'] +>>> f.clean('O2B 2R3') +Traceback (most recent call last): +... +ValidationError: [u'Enter a postal code in the format XXX XXX.'] # CAPhoneNumberField ########################################################## -- You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-upda...@googlegroups.com. To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-updates?hl=en.