File tree Expand file tree Collapse file tree 1 file changed +35
-1
lines changed
Expand file tree Collapse file tree 1 file changed +35
-1
lines changed Original file line number Diff line number Diff line change 1+ import re
2+ from datetime import date
3+
14from django import forms
5+ from django .core .exceptions import ValidationError
26
37from .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
You can’t perform that action at this time.
0 commit comments