28 lines
773 B
Python
28 lines
773 B
Python
import re
|
|
|
|
from django.core.exceptions import ValidationError
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
|
_IRAN_MOBILE_RE = re.compile(r"^09\d{9}$")
|
|
|
|
|
|
def normalize_iran_mobile(value: str) -> str:
|
|
if value is None:
|
|
return ""
|
|
digits = re.sub(r"\D", "", str(value))
|
|
if not digits:
|
|
return ""
|
|
if digits.startswith("0098"):
|
|
digits = "0" + digits[4:]
|
|
elif digits.startswith("98") and len(digits) == 12:
|
|
digits = "0" + digits[2:]
|
|
elif digits.startswith("9") and len(digits) == 10:
|
|
digits = "0" + digits
|
|
return digits
|
|
|
|
|
|
def validate_iran_mobile(value: str) -> None:
|
|
if not _IRAN_MOBILE_RE.match(value or ""):
|
|
raise ValidationError(_("Enter a valid Iranian mobile number (09xxxxxxxxx)."))
|