Merge pull request #1632 from Zykem/identity-age-validation

Identity age validation
This commit is contained in:
Kenshin13
2025-05-20 15:42:03 +02:00
committed by GitHub
2 changed files with 29 additions and 24 deletions
+1 -2
View File
@@ -13,8 +13,7 @@ Config.DateFormat = "DD/MM/YYYY"
Config.MaxNameLength = 20 -- Max Name Length.
Config.MinHeight = 120 -- 120 cm lowest height
Config.MaxHeight = 220 -- 220 cm max height.
Config.LowestYear = 1900 -- 112 years old is the oldest you can be.
Config.HighestYear = 2005 -- 18 years old is the youngest you can be.
Config.MaxAge = 100 -- 100 years old is the oldest you can be.
Config.FullCharDelete = true -- Delete all reference to character.
Config.EnableDebugging = ESX.GetConfig().EnableDebug -- prints for debugging :)
+28 -22
View File
@@ -44,31 +44,37 @@ local function saveIdentityToDatabase(identifier, identity)
MySQL.update.await("UPDATE users SET firstname = ?, lastname = ?, dateofbirth = ?, sex = ?, height = ? WHERE identifier = ?", { identity.firstName, identity.lastName, identity.dateOfBirth, identity.sex, identity.height, identifier })
end
local function checkDOBFormat(str)
str = tostring(str)
if not string.match(str, "(%d%d)/(%d%d)/(%d%d%d%d)") then
---@param year number Year
---@return boolean: true if the year is a leap year, false otherwise
local function isLeapYear(year)
return (year % 4 == 0 and year % 100 ~= 0) or (year % 400 == 0)
end
---@param dob string Date of Birth in the format DD/MM/YYYY
---@return boolean: true if the date is valid, false otherwise
local function checkDOBFormat(dob)
dob = tostring(dob)
local dayStr, monthStr, yearStr = dob:match("^(%d%d?)/(%d%d?)/(%d%d%d%d)$")
if not dayStr or not monthStr or not yearStr then
return false
end
local d, m, y = string.match(str, "(%d+)/(%d+)/(%d+)")
m = tonumber(m)
d = tonumber(d)
y = tonumber(y)
if ((d <= 0) or (d > 31)) or ((m <= 0) or (m > 12)) or ((y <= Config.LowestYear) or (y > Config.HighestYear)) then
local day, month, year = tonumber(dayStr), tonumber(monthStr), tonumber(yearStr)
if not day or not month or not year then
return false
elseif m == 4 or m == 6 or m == 9 or m == 11 then
return d <= 30
elseif m == 2 then
if y % 400 == 0 or (y % 100 ~= 0 and y % 4 == 0) then
return d <= 29
else
return d <= 28
end
else
return d <= 31
end
local currentYear = os.date("*t").year
local minYear = currentYear - Config.MaxAge
local maxYear = currentYear - 18
if year < minYear or year > maxYear then return false end
if month < 1 or month > 12 then return false end
-- Days in each month (starting from January.)
local daysInMonth = { 31, isLeapYear(year) and 29 or 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
return day >= 1 and day <= daysInMonth[month]
end
local function formatDate(str)
@@ -235,7 +241,6 @@ end
ESX.RegisterServerCallback("esx_identity:registerIdentity", function(source, cb, data)
local xPlayer = ESX.GetPlayerFromId(source)
data.dateofbirth = formatDate(data.dateofbirth)
if not checkNameFormat(data.firstname) then
TriggerClientEvent("esx:showNotification", source, TranslateCap("invalid_firstname_format"), "error")
@@ -257,6 +262,7 @@ end
TriggerClientEvent("esx:showNotification", source, TranslateCap("invalid_height_format"), "error")
return cb(false)
end
if xPlayer then
if alreadyRegistered[xPlayer.identifier] then
xPlayer.showNotification(TranslateCap("already_registered"), "error")
@@ -266,7 +272,7 @@ end
playerIdentity[xPlayer.identifier] = {
firstName = formatName(data.firstname),
lastName = formatName(data.lastname),
dateOfBirth = data.dateofbirth,
dateOfBirth = formatDate(data.dateofbirth),
sex = data.sex,
height = data.height,
}