-
-
Save james2doyle/67846afd05335822c149 to your computer and use it in GitHub Desktop.
function validemail(str) | |
if str == nil or str:len() == 0 then return nil end | |
if (type(str) ~= 'string') then | |
error("Expected string") | |
return nil | |
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 or not domainPart:find("%.") then | |
return nil, "Domain is invalid" | |
end | |
if string.sub(domainPart, 1, 1) == "." then | |
return nil, "First character in domain cannot be a dot" | |
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 usage of quotes" | |
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 TLD found in domain" | |
end | |
-- only 1 period in succession allowed | |
if domainPart:find("%.%.") then | |
return nil, "Too many periods in domain" | |
end | |
if localPart:find("%.%.") then | |
return nil, "Too many periods 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 |
Based on wikipedia invalid email addresses, this snippet is return true
if the input [email protected]
.
Add the following rule to fix this:
-- only 1 period in succession allowed
if localPart:find("%.%.") then
return false, "Too many periods in local part"
end
Thanks a lot James (assume this is your name :-) ), adopted your code in my project and am now working perfectly, will let you know if I did any fix in the future.
Best,
Fuxin
Thanks for the script, but I found one small mistake.
If the symbol "@" is at the end of the address, the script breaks.
I found another error. This string validates:
[email protected]
Add this check:
if string.sub(domainPart, 1, 1) == "." then
return false, "first character in domainPart is a dot"
end
The program will stop if string is empty ""
So, I added str:len checker:
if str:len() == 0 then return nil end
One more thing. This code will accept "@gmail.com" but it shouldn't accept it ?
Added a bunch of checks from all the suggestions. Thanks everyone
there is a problem with this code. it returns true to "example@example" string.
i added this code to fix it:
-- no dot found in domain name
if not domainPart:find("%.") then
return nil, "Domain is invalid"
end