-
Notifications
You must be signed in to change notification settings - Fork 0
/
valid-email.lua
62 lines (60 loc) · 2.09 KB
/
valid-email.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
-- based on https://gist.github.com/james2doyle/67846afd05335822c149
module(..., package.seeall)
function validemail(str)
if (type(str) ~= 'string') then
return nil, "Expected string"
end
local lastAt = str:find("[^%@]+$")
if (lastAt == nil) then lastAt = #str + 1 end -- https://gist.github.com/james2doyle/67846afd05335822c149#gistcomment-2568897
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 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 whitespace symbols allowed outside quotes
if localPart:find("%s+") and quotes == nil then
return nil, "Invalid whitespace 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, domainPart
end