In preparation for the release of Raknet in Skyline, I thought I would share these two snippets that show how to validate both an email address and a password. Both are written in pure Lua.
EMAIL ADDRESS
- Code: Select all
function validemail ( str )
if ( str == nil ) then
return nil, "Empty string"
end
if ( type ( str ) ~= "string" ) then
return nil, "Expected a string"
end
local lastAt = str:find ( "[^%@]+$" )
local localPart = str:sub ( 1, ( lastAt - 2 ) ) -- Returns the substring before '@' symbol
local domainPart = str:sub ( lastAt, #str ) -- Returns the substring after '@' symbol
-- we werent able to split the email properly
if ( localPart == nil ) then
return nil, "Local name is invalid"
end
if ( domainPart == nil ) then
return nil, "Domain is invalid"
end
-- local part is maxed at 64 characters
if ( #localPart > 64 ) then
return nil, "Local name must be less than 64 characters"
end
-- domains are maxed at 253 characters
if ( #domainPart > 253 ) then
return nil, "Domain must be less than 253 characters"
end
-- somthing is wrong
if ( lastAt >= 65 ) then
return nil, "Invalid @ symbol usage"
end
-- quotes are only allowed at the beginning of a the local name
local quotes = localPart:find ( "[\"]" )
if ( type ( quotes ) == 'number' and quotes > 1 ) then
return nil, "Invalid quote symbol usage"
end
-- no @ symbols allowed outside quotes
if ( localPart:find ( "%@+" ) and quotes == nil ) then
return nil, "Invalid @ symbol usage in local part"
end
-- no dot found in domain name
if ( not domainPart:find ( "%." ) ) then
return nil, "No period (dot) found in domain"
end
-- only 1 dot in succession allowed
if ( domainPart:find ( "%.%." ) ) then
return nil, "Too many periods (dots) in domain"
end
if ( localPart:find ( "%.%." ) ) then
return nil, "Too many periods (dots) in local part"
end
-- just a general match
if ( not str:match ( '[%w]*[%p]*%@+[%w]*[%.]?[%w]*' ) ) then
return nil, "Email pattern test failed"
end
-- all our tests passed, so we are ok
return true
end
If the email is valid the function returns true and nil, but if it fails, it will return nil and the reason that the validation failed. This code was originally from http://ohdoylerules.com/content/snippets/validate-email-with-lua/
PASSWORD
- Code: Select all
blockedWords = { "password", "letmein", "computer" }
function checkPassword ( Username, Password, minLength, maxLength )
local tStr = ""
local tInt = string.len ( Password )
if ( tInt < minLength ) then
tStr = "Password MUST be longer than " .. minLength .. " characters in length"
return false, tStr
elseif ( tInt > maxLength ) then
tStr = "Password CANNOT be longer than " .. maxLength .. " characters in length"
return false, tStr
end
tStr = string.lower ( Password )
if ( Password == Username or tStr == string.lower ( Username ) ) then
return false, "Password CANNOT be the same (ignoring case) as the Username"
end
for _, value in pairs ( blockedWords ) do
if ( tStr == value ) then
tStr = "Password CANNOT equal " .. value
return false, tStr
end
end
tStr = string.sub ( Password, 1, 1 )
if ( not string.find ( Password, "%a" ) ) then
return false, "Password MUST start with a letter"
end
if ( not string.find ( Password, "%d" ) ) then
return false, "Password MUST contain at least 1 digit"
end
if ( not string.find ( Password, "%l" ) ) then
return false, "Password MUST contain at least 1 lower case letter"
end
if ( not string.find ( Password, "%u" ) ) then
return false, "Password MUST contain at least 1 upper case letter"
end
if ( not string.find ( Password, "[^%a%d]" ) ) then
return false, "Password MUST contain at least 1 special character"
end
end
As with the Email code, this returns true & nil or false & reason. This code was originally from http://wiki.pscs.co.uk/reference:password_strength_checking_script
A couple of notes for the Password code:
- 1) Password must contain at least 1 of each of the following:
- Upper Case Letter
Lower Case Letter
Number
Special Character
3) Password must start with a Letter
4) Password cannot be equal to one of the blockedWords
Hope this is helpful.
Shando
PS: I'm hoping to be able to put together an xml or json file that contains profanities in an assortment of languages, which can be loaded into Skyline before running a check on such things as User Names etc. Using a file will mean that you will be able to select which (if any) language(s) to check.
PPS: Raknet has the ability to register a Profanity Table, so this file can potentially be used with Raknet.