From a1b3877fe428aeefc5bb96f16638d75afb8ef326 Mon Sep 17 00:00:00 2001 From: Andrew4Coding <andrewdevitoaryo@gmail.com> Date: Wed, 19 Mar 2025 00:16:27 +0700 Subject: [PATCH] fix: validation on phone number --- user/models.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/user/models.py b/user/models.py index f28354e..abc2501 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 -- GitLab