from __future__ import unicode_literals

from django.db import models
from django.core.validators import RegexValidator


class PhoneNumberField(models.CharField):
    description = "A field that checks the value be a valid phone number"

    phone_regex = RegexValidator(
        regex=r'^(\+\d{1}\s?)?\(?\d{3}\)?[\s-]?\d{3}[\s-]?\d{4}$', message="Enter a valid Phone Number.")

    def __init__(self, *args, **kwargs):
        if 'max_length' not in kwargs:
            kwargs['max_length'] = 20

        if 'validators' not in kwargs:
            kwargs['validators'] = [self.phone_regex]

        super(PhoneNumberField, self).__init__(*args, **kwargs)

@@ -34,16 +34,27 @@
 terals%0A%0A
+import re%0A%0A
 from dja
@@ -126,16 +126,90 @@
 dator%0A%0A%0A
+US_PHONE_FORMAT = r'%5E(%5C+1%5Cs?)?%5C(?(%5Cd%7B3%7D)%5C)?%5B%5Cs-%5D?(%5Cd%7B3%7D)%5B%5Cs-%5D?(%5Cd%7B4%7D)$'%0A%0A%0A
 class Ph
@@ -369,59 +369,23 @@
 gex=
-r'%5E(%5C+%5Cd%7B1%7D%5Cs?)?%5C(?%5Cd%7B3%7D%5C)?%5B%5Cs-%5D?%5Cd%7B3%7D%5B%5Cs-%5D?%5Cd%7B4%7D$'
+US_PHONE_FORMAT
 , me
@@ -635,16 +635,16 @@
 regex%5D%0A%0A
-
         
@@ -699,8 +699,253 @@
 kwargs)%0A
+%0A    def to_python(self, value):%0A        if value is None:%0A            return value%0A%0A        r = re.match(US_PHONE_FORMAT, value)%0A        if not r:%0A            return value%0A%0A        return %22(%7B%7D) %7B%7D-%7B%7D%22.format(r.group(2), r.group(3), r.group(4))%0A

Standardize Phone number format