# stdlib
import re
# django
from django.forms.fields import EMPTY_VALUES, Field
from django.forms import ValidationError
from django.utils.encoding import smart_unicode
__all__ = ('phone_digits_re', 'USPhoneNumberField')
phone_digits_re = re.compile(r'^((\((?P<areacode_1>\d{3})\))|(?P<areacode_2>\d{3}))[ \-]*(?P<first>\d{3})[ \-]*(?P<last>\d{4})$')
class USPhoneNumberField(Field):
" A USPhoneNumberField that accepts a wide variety of valid phone number patterns. "
default_error_messages = {
'invalid': u'The phone number is invalid.',
}
def clean(self, value):
super(USPhoneNumberField, self).clean(value)
if value in EMPTY_VALUES:
return u''
m = phone_digits_re.match( smart_unicode(value) )
if m:
return u'%s-%s-%s' % (m.group('areacode_1') or m.group('areacode_2'), m.group('first'), m.group('last'))
raise ValidationError(self.error_messages['invalid'])
Comments
Hello I am trying to implement but where do I place this code? in the Models.py or views.py ? Thank you for the Code
#