-
Notifications
You must be signed in to change notification settings - Fork 316
/
validators.py
24 lines (19 loc) · 825 Bytes
/
validators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import phonenumbers
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _
from phonenumber_field.phonenumber import PhoneNumber, to_python
def validate_international_phonenumber(value):
phone_number = to_python(value)
if isinstance(phone_number, PhoneNumber) and not phone_number.is_valid():
raise ValidationError(
_("The phone number entered is not valid."), code="invalid"
)
def validate_phonenumber(value):
phone_number = to_python(value)
if isinstance(phone_number, PhoneNumber) and not (
phonenumbers.is_valid_number(phone_number)
or phonenumbers.is_valid_short_number(phone_number)
):
raise ValidationError(
_("The phone number entered is not valid."), code="invalid"
)