@@ -44,31 +44,51 @@ local function saveIdentityToDatabase(identifier, identity)
4444 MySQL .update .await (" UPDATE users SET firstname = ?, lastname = ?, dateofbirth = ?, sex = ?, height = ? WHERE identifier = ?" , { identity .firstName , identity .lastName , identity .dateOfBirth , identity .sex , identity .height , identifier })
4545end
4646
47- local function checkDOBFormat (str )
48- str = tostring (str )
49- if not string.match (str , " (%d%d)/(%d%d)/(%d%d%d%d)" ) then
47+ --- @param year number Year
48+ --- @return boolean : true if the year is a leap year , false otherwise
49+ local function isLeapYear (year )
50+ return (year % 4 == 0 and year % 100 ~= 0 ) or (year % 400 == 0 )
51+ end
52+
53+ --- @param dob string Date of Birth in the format DD /MM /YYYY
54+ --- @return boolean : true if the date is valid , false otherwise
55+ local function checkDOBFormat (dob )
56+ dob = tostring (dob )
57+
58+ local dayStr , monthStr , yearStr = dob :match (' ^(%d%d?)/(%d%d?)/(%d%d%d%d)$' )
59+ if not dayStr or not monthStr or not yearStr then
5060 return false
5161 end
5262
53- local d , m , y = string.match (str , " (%d+)/(%d+)/(%d+)" )
63+ local day , month , year = tonumber (dayStr ), tonumber (monthStr ), tonumber (yearStr )
64+ if not day or not month or not year then
65+ return false
66+ end
5467
55- m = tonumber (m )
56- d = tonumber (d )
57- y = tonumber (y )
68+ local currentDate = os.date (" *t" )
69+ local currentYear = currentDate .year
70+ local minYear = currentYear - Config .MaxAge
71+ local maxYear = currentYear - Config .MinAge
5872
59- if (( d <= 0 ) or ( d > 31 )) or (( m <= 0 ) or ( m > 12 )) or (( y <= Config . LowestYear ) or ( y > Config . HighestYear )) then
73+ if year < minYear or year > maxYear or year > currentYear then
6074 return false
61- elseif m == 4 or m == 6 or m == 9 or m == 11 then
62- return d <= 30
63- elseif m == 2 then
64- if y % 400 == 0 or (y % 100 ~= 0 and y % 4 == 0 ) then
65- return d <= 29
66- else
67- return d <= 28
75+ end
76+
77+ if year == maxYear then
78+ if month > currentDate .month or (month == currentDate .month and day > currentDate .day ) then
79+ return false
6880 end
69- else
70- return d <= 31
7181 end
82+
83+ if month < 1 or month > 12 then return false end
84+
85+ -- Days in each month (starting from January.)
86+ local daysInMonth = { 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 }
87+ if month == 2 and isLeapYear (year ) then
88+ daysInMonth [2 ] = 29
89+ end
90+
91+ return day >= 1 and day <= daysInMonth [month ]
7292end
7393
7494local function formatDate (str )
0 commit comments