Files
2026-10-08 16:37:45 +03:30

15 lines
749 B
Python

from django.core.exceptions import ValidationError
def normalize_mobile(value):
"""Keep the integer storage format while accepting Iranian mobile formats."""
mobile = str(value).strip().translate(str.maketrans("۰۱۲۳۴۵۶۷۸۹٠١٢٣٤٥٦٧٨٩", "01234567890123456789"))
mobile = mobile.replace(" ", "").replace("-", "")
for prefix in ("+98", "0098", "98", "0"):
if mobile.startswith(prefix) and len(mobile) == 10 + len(prefix):
mobile = mobile[len(prefix):]
break
if len(mobile) != 10 or not mobile.isascii() or not mobile.isdigit() or not mobile.startswith("9"):
raise ValidationError({"mobile": "شماره موبایل معتبر وارد کنید."})
return int(mobile)