| 1 | import re |
| 2 | |
| 3 | from django.core.validators import EMPTY_VALUES |
| 4 | from django.forms import CharField |
| 5 | from django.forms import ValidationError |
| 6 | from django.utils.encoding import smart_unicode |
| 7 | |
| 8 | |
| 9 | hk_phone_digits_re = re.compile(r'^(?:852-?)?(\d{4})[-\.]?(\d{4})$') |
| 10 | hk_special_numbers = ('999', '992', '112') |
| 11 | hk_phone_prefixes = ('2', '3', '5', '6', '8', '9') |
| 12 | |
| 13 | |
| 14 | class HKPhoneNumberField(CharField): |
| 15 | """ |
| 16 | Validate Hong Kong phone number. |
| 17 | The input format can be either one of the followings: |
| 18 | 'XXXX-XXXX', '852-XXXX-XXXX', '(+852) XXXX-XXXX', |
| 19 | 'XXXX XXXX', or 'XXXXXXXX'. |
| 20 | The output format is 'XXXX-XXXX'. |
| 21 | |
| 22 | Note: The phone number shall not start with 999, 992, or 112. |
| 23 | And, it should start with either 2, 3, 5, 6, 8, or 9. |
| 24 | |
| 25 | Ref - http://en.wikipedia.org/wiki/Telephone_numbers_in_Hong_Kong |
| 26 | """ |
| 27 | default_error_messages = { |
| 28 | 'disguise': 'Phone number should not start with ' \ |
| 29 | 'one of the followings: %s.' % \ |
| 30 | ', '.join(hk_special_numbers), |
| 31 | 'invalid': 'Phone number must be in XXXX-XXXX format.', |
| 32 | 'prefix': 'Phone number should start with ' \ |
| 33 | 'one of the followings: %s.' % \ |
| 34 | ', '.join(hk_phone_prefixes), |
| 35 | } |
| 36 | |
| 37 | def __init__(self, *args, **kwargs): |
| 38 | super(HKPhoneNumberField, self).__init__(*args, **kwargs) |
| 39 | |
| 40 | def clean(self, value): |
| 41 | super(HKPhoneNumberField, self).clean(value) |
| 42 | |
| 43 | if value in EMPTY_VALUES: |
| 44 | return u'' |
| 45 | |
| 46 | value = re.sub('(\(|\)|\s+|\+)', '', smart_unicode(value)) |
| 47 | m = hk_phone_digits_re.search(value) |
| 48 | if not m: |
| 49 | raise ValidationError(self.error_messages['invalid']) |
| 50 | |
| 51 | value = u'%s-%s' % (m.group(1), m.group(2)) |
| 52 | for special in hk_special_numbers: |
| 53 | if value.startswith(special): |
| 54 | raise ValidationError(self.error_messages['disguise']) |
| 55 | |
| 56 | prefix_found = map(lambda prefix: value.startswith(prefix), |
| 57 | hk_phone_prefixes) |
| 58 | if not any(prefix_found): |
| 59 | raise ValidationError(self.error_messages['prefix']) |
| 60 | |
| 61 | return value |