app/django/contrib/localflavor/us/forms.py
changeset 323 ff1a9aa48cfd
parent 54 03e267d67478
equal deleted inserted replaced
322:6641e941ef1e 323:ff1a9aa48cfd
     1 """
     1 """
     2 USA-specific Form helpers
     2 USA-specific Form helpers
     3 """
     3 """
     4 
     4 
     5 from django.newforms import ValidationError
     5 from django.forms import ValidationError
     6 from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES
     6 from django.forms.fields import Field, RegexField, Select, EMPTY_VALUES
     7 from django.utils.encoding import smart_unicode
     7 from django.utils.encoding import smart_unicode
     8 from django.utils.translation import ugettext
     8 from django.utils.translation import ugettext_lazy as _
     9 import re
     9 import re
    10 
    10 
    11 phone_digits_re = re.compile(r'^(?:1-?)?(\d{3})[-\.]?(\d{3})[-\.]?(\d{4})$')
    11 phone_digits_re = re.compile(r'^(?:1-?)?(\d{3})[-\.]?(\d{3})[-\.]?(\d{4})$')
    12 ssn_re = re.compile(r"^(?P<area>\d{3})[-\ ]?(?P<group>\d{2})[-\ ]?(?P<serial>\d{4})$")
    12 ssn_re = re.compile(r"^(?P<area>\d{3})[-\ ]?(?P<group>\d{2})[-\ ]?(?P<serial>\d{4})$")
    13 
    13 
    14 class USZipCodeField(RegexField):
    14 class USZipCodeField(RegexField):
    15     default_error_messages = {
    15     default_error_messages = {
    16         'invalid': ugettext('Enter a zip code in the format XXXXX or XXXXX-XXXX.'),
    16         'invalid': _('Enter a zip code in the format XXXXX or XXXXX-XXXX.'),
    17     }
    17     }
    18 
    18 
    19     def __init__(self, *args, **kwargs):
    19     def __init__(self, *args, **kwargs):
    20         super(USZipCodeField, self).__init__(r'^\d{5}(?:-\d{4})?$',
    20         super(USZipCodeField, self).__init__(r'^\d{5}(?:-\d{4})?$',
    21             max_length=None, min_length=None, *args, **kwargs)
    21             max_length=None, min_length=None, *args, **kwargs)
    49         * The number is not one known to be invalid due to otherwise widespread
    49         * The number is not one known to be invalid due to otherwise widespread
    50           promotional use or distribution (e.g., the Woolworth's number or the
    50           promotional use or distribution (e.g., the Woolworth's number or the
    51           1962 promotional number).
    51           1962 promotional number).
    52     """
    52     """
    53     default_error_messages = {
    53     default_error_messages = {
    54         'invalid': ugettext('Enter a valid U.S. Social Security number in XXX-XX-XXXX format.'),
    54         'invalid': _('Enter a valid U.S. Social Security number in XXX-XX-XXXX format.'),
    55     }
    55     }
    56 
    56 
    57     def clean(self, value):
    57     def clean(self, value):
    58         super(USSocialSecurityNumberField, self).clean(value)
    58         super(USSocialSecurityNumberField, self).clean(value)
    59         if value in EMPTY_VALUES:
    59         if value in EMPTY_VALUES: