Ticket #17355: nanp-proper-validation.patch

File nanp-proper-validation.patch, 4.9 KB (added by Dan McGee, 12 years ago)
  • tests/regressiontests/localflavor/us/tests.py

     
    246246        error_format = [u'Phone numbers must be in XXX-XXX-XXXX format.']
    247247        valid = {
    248248            '312-555-1212': '312-555-1212',
     249            '1-312-555-1212': '312-555-1212',
    249250            '3125551212': '312-555-1212',
    250251            '312 555-1212': '312-555-1212',
    251252            '(312) 555-1212': '312-555-1212',
     
    257258        invalid = {
    258259            '555-1212': error_format,
    259260            '312-55-1212': error_format,
     261            '(123) 234 5678': error_format,
     262            # http://en.wikipedia.org/wiki/North_American_Numbering_Plan
     263            # No leading 0/1, no N11 codes, etc.
     264            '023-234-5678': error_format,
     265            '123-234-5678': error_format,
     266            '323-134-5678': error_format,
     267            '312-034-5678': error_format,
     268            '311-134-5678': error_format,
     269            '312-211-5678': error_format,
    260270        }
    261271        self.assertFieldOutput(USPhoneNumberField, valid, invalid)
    262272
  • tests/regressiontests/localflavor/ca/tests.py

     
    7272        error_format = [u'Phone numbers must be in XXX-XXX-XXXX format.']
    7373        valid = {
    7474            '403-555-1212': '403-555-1212',
     75            '1-403-555-1212': '403-555-1212',
    7576            '4035551212': '403-555-1212',
    7677            '403 555-1212': '403-555-1212',
    7778            '(403) 555-1212': '403-555-1212',
     
    8182            ' (403) 555.1212 ': '403-555-1212',
    8283        }
    8384        invalid = {
    84            '555-1212': error_format,
    85            '403-55-1212': error_format,
     85            '555-1212': error_format,
     86            '403-55-1212': error_format,
     87            '(123) 234 5678': error_format,
     88            # http://en.wikipedia.org/wiki/North_American_Numbering_Plan
     89            # No leading 0/1, no N11 codes, etc.
     90            '023-234-5678': error_format,
     91            '123-234-5678': error_format,
     92            '403-134-5678': error_format,
     93            '403-034-5678': error_format,
     94            '411-134-5678': error_format,
     95            '403-211-5678': error_format,
    8696        }
    8797        self.assertFieldOutput(CAPhoneNumberField, valid, invalid)
    8898
  • django/contrib/localflavor/us/forms.py

     
    1313from django.utils.translation import ugettext_lazy as _
    1414
    1515
    16 phone_digits_re = re.compile(r'^(?:1-?)?(\d{3})[-\.]?(\d{3})[-\.]?(\d{4})$')
     16phone_digits_re = re.compile(r'^(?:1-?)?([2-9]\d{2})[-\.]?([2-9]\d{2})[-\.]?(\d{4})$')
    1717ssn_re = re.compile(r"^(?P<area>\d{3})[-\ ]?(?P<group>\d{2})[-\ ]?(?P<serial>\d{4})$")
    1818
    1919class USZipCodeField(RegexField):
     
    3737        value = re.sub('(\(|\)|\s+)', '', smart_unicode(value))
    3838        m = phone_digits_re.search(value)
    3939        if m:
     40            # http://en.wikipedia.org/wiki/North_American_Numbering_Plan
     41            # ensure last 2 digits of area code or exchange are not both '1'
     42            if (m.group(1)[1] == '1' and m.group(1)[2] == '1') or \
     43                    (m.group(2)[1] == '1' and m.group(2)[2] == '1'):
     44                raise ValidationError(self.error_messages['invalid'])
    4045            return u'%s-%s-%s' % (m.group(1), m.group(2), m.group(3))
    4146        raise ValidationError(self.error_messages['invalid'])
    4247
  • django/contrib/localflavor/ca/forms.py

     
    1313from django.utils.translation import ugettext_lazy as _
    1414
    1515
    16 phone_digits_re = re.compile(r'^(?:1-?)?(\d{3})[-\.]?(\d{3})[-\.]?(\d{4})$')
     16phone_digits_re = re.compile(r'^(?:1-?)?([2-9]\d{2})[-\.]?([2-9]\d{2})[-\.]?(\d{4})$')
    1717sin_re = re.compile(r"^(\d{3})-(\d{3})-(\d{3})$")
    1818
    1919class CAPostalCodeField(CharField):
     
    5656        value = re.sub('(\(|\)|\s+)', '', smart_unicode(value))
    5757        m = phone_digits_re.search(value)
    5858        if m:
     59            # http://en.wikipedia.org/wiki/North_American_Numbering_Plan
     60            # ensure last 2 digits of area code or exchange are not both '1'
     61            if (m.group(1)[1] == '1' and m.group(1)[2] == '1') or \
     62                    (m.group(2)[1] == '1' and m.group(2)[2] == '1'):
     63                raise ValidationError(self.error_messages['invalid'])
    5964            return u'%s-%s-%s' % (m.group(1), m.group(2), m.group(3))
    6065        raise ValidationError(self.error_messages['invalid'])
    6166
Back to Top