| 115 | class ARPhoneNumberField(RegexField): |
| 116 | """ |
| 117 | Argentina phone numbers field. |
| 118 | """ |
| 119 | default_error_messages = { |
| 120 | 'invalid': _('Phone numbers must be in XXXX-XXXXXX format.'), |
| 121 | } |
| 122 | |
| 123 | def __init__(self, *args, **kwargs): |
| 124 | super(ARPhoneNumberField, self).__init__(r'^[0-9\-\(\)\s]{6,16}$', |
| 125 | max_length=16, min_length=6, *args, **kwargs) |
| 126 | |
| 127 | def clean(self, value): |
| 128 | """ |
| 129 | Strips -, (, ) and spaces. Checks the final length. |
| 130 | """ |
| 131 | value = super(ARPhoneNumberField, self).clean(value) |
| 132 | value = value.replace('-','') |
| 133 | value = value.replace('(','') |
| 134 | value = value.replace(')','') |
| 135 | value = value.replace(' ','') |
| 136 | if len(value) != 10: |
| 137 | raise ValidationError(self.error_messages['invalid']) |
| 138 | return value |