diff --git a/user/models.py b/user/models.py index f28354ed1989ad3b5e1e7062f69bfc8064fac98d..abc250104303e58b61ed1dd5ed50460a4f6145a3 100644 --- a/user/models.py +++ b/user/models.py @@ -15,14 +15,20 @@ class ExtendedUser(AbstractUser): def validate_and_sanitize_phone(value): + # check for length + if not (8 <= len(value) <= 15): + raise ValidationError('Nomor telepon harus memiliki panjang total antara 8 hingga 15 angka.') + phone_regex = RegexValidator( - regex=r'^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$', - message='Nomor telepon tidak valid.' + regex=r'^\+?\d{1,3}-?\d{4,15}$', + message='Nomor telepon harus memiliki format kode negara - nomor telepon atau nomor telepon dengan tanda hubung opsional.' ) phone_regex(value) - # If Success, sanitize input by removing unnecessary characters (+ and -) - value = value.replace('(', '').replace(')', '').replace('-', '').replace(' ', '') + # Check total length (excluding + and -) + sanitized_value = value.replace('+', '').replace('-', '') + if not (8 <= len(sanitized_value) <= 15): + raise ValidationError('Nomor telepon harus memiliki panjang total antara 8 hingga 15 angka.') return value