Ticket #17355: nanp-proper-validation.patch
File nanp-proper-validation.patch, 4.9 KB (added by , 13 years ago) |
---|
-
tests/regressiontests/localflavor/us/tests.py
246 246 error_format = [u'Phone numbers must be in XXX-XXX-XXXX format.'] 247 247 valid = { 248 248 '312-555-1212': '312-555-1212', 249 '1-312-555-1212': '312-555-1212', 249 250 '3125551212': '312-555-1212', 250 251 '312 555-1212': '312-555-1212', 251 252 '(312) 555-1212': '312-555-1212', … … 257 258 invalid = { 258 259 '555-1212': error_format, 259 260 '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, 260 270 } 261 271 self.assertFieldOutput(USPhoneNumberField, valid, invalid) 262 272 -
tests/regressiontests/localflavor/ca/tests.py
72 72 error_format = [u'Phone numbers must be in XXX-XXX-XXXX format.'] 73 73 valid = { 74 74 '403-555-1212': '403-555-1212', 75 '1-403-555-1212': '403-555-1212', 75 76 '4035551212': '403-555-1212', 76 77 '403 555-1212': '403-555-1212', 77 78 '(403) 555-1212': '403-555-1212', … … 81 82 ' (403) 555.1212 ': '403-555-1212', 82 83 } 83 84 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, 86 96 } 87 97 self.assertFieldOutput(CAPhoneNumberField, valid, invalid) 88 98 -
django/contrib/localflavor/us/forms.py
13 13 from django.utils.translation import ugettext_lazy as _ 14 14 15 15 16 phone_digits_re = re.compile(r'^(?:1-?)?( \d{3})[-\.]?(\d{3})[-\.]?(\d{4})$')16 phone_digits_re = re.compile(r'^(?:1-?)?([2-9]\d{2})[-\.]?([2-9]\d{2})[-\.]?(\d{4})$') 17 17 ssn_re = re.compile(r"^(?P<area>\d{3})[-\ ]?(?P<group>\d{2})[-\ ]?(?P<serial>\d{4})$") 18 18 19 19 class USZipCodeField(RegexField): … … 37 37 value = re.sub('(\(|\)|\s+)', '', smart_unicode(value)) 38 38 m = phone_digits_re.search(value) 39 39 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']) 40 45 return u'%s-%s-%s' % (m.group(1), m.group(2), m.group(3)) 41 46 raise ValidationError(self.error_messages['invalid']) 42 47 -
django/contrib/localflavor/ca/forms.py
13 13 from django.utils.translation import ugettext_lazy as _ 14 14 15 15 16 phone_digits_re = re.compile(r'^(?:1-?)?( \d{3})[-\.]?(\d{3})[-\.]?(\d{4})$')16 phone_digits_re = re.compile(r'^(?:1-?)?([2-9]\d{2})[-\.]?([2-9]\d{2})[-\.]?(\d{4})$') 17 17 sin_re = re.compile(r"^(\d{3})-(\d{3})-(\d{3})$") 18 18 19 19 class CAPostalCodeField(CharField): … … 56 56 value = re.sub('(\(|\)|\s+)', '', smart_unicode(value)) 57 57 m = phone_digits_re.search(value) 58 58 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']) 59 64 return u'%s-%s-%s' % (m.group(1), m.group(2), m.group(3)) 60 65 raise ValidationError(self.error_messages['invalid']) 61 66