26 lines
852 B
Python
26 lines
852 B
Python
from django.core.validators import RegexValidator
|
|
from django.utils.translation import gettext_lazy as _
|
|
from django.core.exceptions import ValidationError
|
|
import re
|
|
|
|
|
|
class SaudiPhoneNumberValidator(RegexValidator):
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(
|
|
regex=r"^(\+9665|05|9665)[0-9]{8}$",
|
|
message=_("Enter a valid Saudi phone number (05XXXXXXXX or +9665XXXXXXXX)"),
|
|
)
|
|
|
|
def __call__(self, value):
|
|
# Remove any whitespace, dashes, or other separators
|
|
cleaned_value = re.sub(r"[\s\-\(\)\.]", "", str(value))
|
|
super().__call__(cleaned_value)
|
|
|
|
|
|
def vat_rate_validator(value):
|
|
if value < 0 or value > 1:
|
|
raise ValidationError(
|
|
_("%(value)s is not a valid VAT rate. It must be between 0 and 1."),
|
|
params={"value": value},
|
|
)
|