Created
August 8, 2011 22:13
-
-
Save jirwin/1132879 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class USPhoneNumber(FancyValidator): | |
""" | |
Validates, and converts to ###-###-####, optionally with extension | |
(as ext.##...). Only support US phone numbers. See | |
InternationalPhoneNumber for support for that kind of phone number. | |
:: | |
>>> p = USPhoneNumber() | |
>>> p.to_python('333-3333') | |
Traceback (most recent call last): | |
... | |
Invalid: Please enter a number, with area code, in the form ###-###-####, optionally with "ext.####" | |
>>> p.to_python('555-555-5555') | |
'555-555-5555' | |
>>> p.to_python('1-393-555-3939') | |
'1-393-555-3939' | |
>>> p.to_python('321.555.4949') | |
'321.555.4949' | |
>>> p.to_python('3335550000') | |
'3335550000' | |
""" | |
# for emacs: " | |
_phoneRE = re.compile(r'^\s*(?:1-)?(\d\d\d)[\- \.]?(\d\d\d)[\- \.]?(\d\d\d\d)(?:\s*ext\.?\s*(\d+))?\s*$', re.I) | |
messages = { | |
'phoneFormat': _('Please enter a number, with area code, in the form ###-###-####, optionally with "ext.####"'), | |
} | |
def _to_python(self, value, state): | |
self.assert_string(value, state) | |
match = self._phoneRE.search(value) | |
if not match: | |
raise Invalid( | |
self.message('phoneFormat', state), | |
value, state) | |
return value | |
def _from_python(self, value, state): | |
self.assert_string(value, state) | |
match = self._phoneRE.search(value) | |
if not match: | |
raise Invalid(self.message('phoneFormat', state), | |
value, state) | |
result = '%s-%s-%s' % (match.group(1), match.group(2), match.group(3)) | |
if match.group(4): | |
result = result + " ext.%s" % match.group(4) | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment