Ticket #2307: phone.patch

File phone.patch, 2.1 KB (added by anjohnson@…, 18 years ago)

Patch to fix the shortcomings discussed in ticket.

  • django/forms/__init__.py

     
    891891        SelectField.__init__(self, field_name, choices, 1, is_required, validator_list)
    892892
    893893class PhoneNumberField(TextField):
    894     "A convenience FormField for validating phone numbers (e.g. '630-555-1234')"
     894    "A convenience FormField for validating phone numbers (e.g. '(630) 555-1234')"
    895895    def __init__(self, field_name, is_required=False, validator_list=[]):
    896896        validator_list = [self.isValidPhone] + validator_list
    897         TextField.__init__(self, field_name, length=12, maxlength=12,
     897        TextField.__init__(self, field_name, length=20, maxlength=20,
    898898            is_required=is_required, validator_list=validator_list)
    899899
    900900    def isValidPhone(self, field_data, all_data):
  • django/core/validators.py

     
    2323email_re = re.compile(r'^[A-Z0-9._%-][+A-Z0-9._%-]*@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$', re.IGNORECASE)
    2424integer_re = re.compile(r'^-?\d+$')
    2525ip4_re = re.compile(r'^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$')
    26 phone_re = re.compile(r'^[A-PR-Y0-9]{3}-[A-PR-Y0-9]{3}-[A-PR-Y0-9]{4}$', re.IGNORECASE)
     26phone_re = re.compile(r'^((1-)?[A-PR-Y0-9]{3}|\([A-PR-Y0-9]{3}\))[ -]?[A-PR-Y0-9]{3}[ -]?[A-PR-Y0-9]{4}$', re.IGNORECASE)
    2727slug_re = re.compile(r'^[-\w]+$')
    2828url_re = re.compile(r'^https?://\S+$')
    2929
     
    156156
    157157def isValidPhone(field_data, all_data):
    158158    if not phone_re.search(field_data):
    159         raise ValidationError, gettext('Phone numbers must be in XXX-XXX-XXXX format. "%s" is invalid.') % field_data
     159        raise ValidationError, gettext('Phone numbers must be in XXX-XXX-XXXX or (XXX) XXX-XXXX formats. "%s" is invalid.') % field_data
    160160
    161161def isValidQuicktimeVideoURL(field_data, all_data):
    162162    "Checks that the given URL is a video that can be played by QuickTime (qt, mpeg)"
Back to Top