feat: 手机号支持选择区号

This commit is contained in:
jiangweidong
2023-04-07 21:25:26 +08:00
committed by Jiangjie.Bai
parent ec2c8538d9
commit 7833433d5f
3 changed files with 15 additions and 2 deletions

View File

@@ -2,6 +2,8 @@
#
import re
import phonenumbers
from django.core.validators import RegexValidator
from django.utils.translation import ugettext_lazy as _
from rest_framework.validators import (
@@ -42,9 +44,9 @@ class NoSpecialChars:
class PhoneValidator:
pattern = re.compile(r"^1[3456789]\d{9}$")
message = _('The mobile phone number format is incorrect')
def __call__(self, value):
if not self.pattern.match(value):
phone = phonenumbers.parse(value)
if not phonenumbers.is_valid_number(phone):
raise serializers.ValidationError(self.message)