1 | 7c7
|
---|
2 | < from django.forms.fields import Field, RegexField, Select
|
---|
3 | ---
|
---|
4 | > from django.forms.fields import Field, RegexField, Select, CharField
|
---|
5 | 9c9
|
---|
6 | < from django.utils.translation import gettext
|
---|
7 | ---
|
---|
8 | > from django.utils.translation import ugettext_lazy as _
|
---|
9 | 15c15
|
---|
10 | < 'invalid': gettext(u'Enter a zip code in the format XXXXXXX.'),
|
---|
11 | ---
|
---|
12 | > 'invalid': _(u'Enter a zip code in the format XXXXXXX.'),
|
---|
13 | 56a57,93
|
---|
14 | > #thanks to Steve Fernandez for neatly formatting this code
|
---|
15 | > phone_digits_re = re.compile(r"""
|
---|
16 | > (
|
---|
17 | > (?P<std_code> # the std-code group
|
---|
18 | > ^0 # all std-codes start with 0
|
---|
19 | > (
|
---|
20 | > (?P<twodigit>\d{2}) | # either two, three or four digits
|
---|
21 | > (?P<threedigit>\d{3}) | # following the 0
|
---|
22 | > (?P<fourdigit>\d{4})
|
---|
23 | > )
|
---|
24 | > )
|
---|
25 | > [-\s] # space or -
|
---|
26 | > (?P<phone_no> # the phone number group
|
---|
27 | > [1-6] # first digit of phone number
|
---|
28 | > (
|
---|
29 | > (?(twodigit)\d{7}) | # 7 more phone digits for 3 digit stdcode
|
---|
30 | > (?(threedigit)\d{6}) | # 6 more phone digits for 4 digit stdcode
|
---|
31 | > (?(fourdigit)\d{5}) # 5 more phone digits for 5 digit stdcode
|
---|
32 | > )
|
---|
33 | > )
|
---|
34 | > )$""", re.VERBOSE)
|
---|
35 | >
|
---|
36 | > class INPhoneNumberField(CharField):
|
---|
37 | > default_error_messages = {
|
---|
38 | > 'invalid': _('Phone numbers must be in 02X-7X or 03X-6X or 04X-5X format.'),
|
---|
39 | > }
|
---|
40 | >
|
---|
41 | > def clean(self, value):
|
---|
42 | > super(INPhoneNumberField, self).clean(value)
|
---|
43 | > if value in EMPTY_VALUES:
|
---|
44 | > return u''
|
---|
45 | > value = smart_unicode(value)
|
---|
46 | > m = phone_digits_re.match(value)
|
---|
47 | > if m:
|
---|
48 | > return u'%s' % (value)
|
---|
49 | > raise ValidationError(self.error_messages['invalid'])
|
---|
50 | >
|
---|