Skip to content

Commit 336814e

Browse files
Merge branch 'master' into ui-changes
2 parents fe30182 + f7871c4 commit 336814e

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

Algolyzer/home/forms.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
import re
2+
from datetime import date
3+
14
from django import forms
5+
from django.core.exceptions import ValidationError
26

37
from .models import UserProfile
48

@@ -22,7 +26,7 @@ class OnboardingForm(forms.ModelForm):
2226
attrs={"class": "form-control", "placeholder": "+1234567890"}
2327
),
2428
label="Phone Number",
25-
max_length=15,
29+
max_length=15, # Increased to accommodate country codes
2630
required=True,
2731
)
2832

@@ -48,3 +52,33 @@ class Meta:
4852
"gender",
4953
"address",
5054
]
55+
56+
def clean_phone_number(self):
57+
phone_number = self.cleaned_data.get("phone_number")
58+
59+
# Remove all non-digit characters
60+
# cleaned_number = re.sub(r'[^\d+]', '', phone_number)
61+
62+
# Check if the number contains only digits and optional + at start
63+
if not re.match(r"^\+?\d+$", phone_number):
64+
raise ValidationError(
65+
"Phone number must contain only numbers and an optional + at the start."
66+
)
67+
68+
# Check minimum length (adjust according to your requirements)
69+
if len(phone_number) < 10:
70+
raise ValidationError("Phone number must be at least 10 digits long.")
71+
72+
return phone_number
73+
74+
def clean_dob(self):
75+
dob = self.cleaned_data.get("dob")
76+
today = date.today()
77+
78+
# Calculate age
79+
age = today.year - dob.year - ((today.month, today.day) < (dob.month, dob.day))
80+
81+
if age < 12:
82+
raise ValidationError("You must be at least 12 years old to register.")
83+
84+
return dob

0 commit comments

Comments
 (0)