mirror of
https://github.com/esx-framework/esx_core.git
synced 2026-08-30 17:58:54 +00:00
Improved form handling with verifying data
This commit is contained in:
+87
-4
@@ -1,5 +1,13 @@
|
||||
local guiEnabled = false
|
||||
local myIdentity = {}
|
||||
ESX = nil
|
||||
|
||||
Citizen.CreateThread(function()
|
||||
while ESX == nil do
|
||||
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
|
||||
Citizen.Wait(0)
|
||||
end
|
||||
end)
|
||||
|
||||
function EnableGui(enable)
|
||||
SetNuiFocus(enable)
|
||||
@@ -21,11 +29,42 @@ RegisterNUICallback('escape', function(data, cb)
|
||||
end)
|
||||
|
||||
RegisterNUICallback('register', function(data, cb)
|
||||
local reason = ""
|
||||
myIdentity = data
|
||||
TriggerServerEvent('esx_identity:setIdentity', data)
|
||||
EnableGui(false)
|
||||
Citizen.Wait(500)
|
||||
TriggerEvent('esx_skin:openSaveableMenu')
|
||||
for theData, value in pairs(myIdentity) do
|
||||
if theData == "firstname" or theData == "lastname" then
|
||||
reason = verifyName(value)
|
||||
|
||||
if reason ~= "" then
|
||||
break
|
||||
end
|
||||
elseif theData == "dateofbirth" then
|
||||
if value == "invalid" then
|
||||
reason = "Invalid date of birth!"
|
||||
break
|
||||
end
|
||||
elseif theData == "height" then
|
||||
local height = tonumber(value)
|
||||
if height then
|
||||
if height > 200 or height < 140 then
|
||||
reason = "Unacceptable player height!"
|
||||
break
|
||||
end
|
||||
else
|
||||
reason = "Unacceptable player height!"
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if reason == "" then
|
||||
--TriggerServerEvent('esx_identity:setIdentity', data)
|
||||
EnableGui(false)
|
||||
Citizen.Wait(500)
|
||||
--TriggerEvent('esx_skin:openSaveableMenu')
|
||||
else
|
||||
ESX.ShowNotification(reason)
|
||||
end
|
||||
end)
|
||||
|
||||
Citizen.CreateThread(function()
|
||||
@@ -44,3 +83,47 @@ Citizen.CreateThread(function()
|
||||
Citizen.Wait(10)
|
||||
end
|
||||
end)
|
||||
|
||||
function verifyName(name)
|
||||
-- Don't allow short user names
|
||||
local nameLength = string.len(name)
|
||||
if nameLength > 25 or nameLength < 5 then
|
||||
return 'Your player name is either too short or too long.'
|
||||
end
|
||||
|
||||
-- Don't allow special characters (doesn't always work)
|
||||
local count = 0
|
||||
for i in name:gmatch('[abcdefghijklmnopqrstuvwxyzåäöABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ0123456789 -]') do
|
||||
count = count + 1
|
||||
end
|
||||
if count ~= nameLength then
|
||||
return 'Your player name contains special characters that are not allowed on this server.'
|
||||
end
|
||||
|
||||
-- Does the player carry a first and last name?
|
||||
--
|
||||
-- Example:
|
||||
-- Allowed: 'Bob Joe'
|
||||
-- Not allowed: 'Bob'
|
||||
-- Not allowed: 'Bob joe'
|
||||
local spacesInName = 0
|
||||
local spacesWithUpper = 0
|
||||
for word in string.gmatch(name, '%S+') do
|
||||
|
||||
if string.match(word, '%u') then
|
||||
spacesWithUpper = spacesWithUpper + 1
|
||||
end
|
||||
|
||||
spacesInName = spacesInName + 1
|
||||
end
|
||||
|
||||
if spacesInName > 2 then
|
||||
return 'Your name contains more than two spaces'
|
||||
end
|
||||
|
||||
if spacesWithUpper ~= spacesInName then
|
||||
return 'your name must start with a capital letter.'
|
||||
end
|
||||
|
||||
return ''
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user