Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Test if String is E-Mail Address

Little Gray
Registered User
Join date: 16 Oct 2006
Posts: 48
08-28-2007 17:15
I need to test if a string is an e-mail address (not whether a string is the address of a valid e-mail account; just whether a string will be accepted by a mail server for attempted delivery to the address). I've written a simple function for doing this and would appreciate it if anyone has comments on how to make it more robust, more efficient, etc.

Here's the function:

integer IsEmailAddr(string test)
{
list testlist = llParseString2List(test,[" "],[]); // check for spaces
if ( llGetListLength(testlist) > 1 ) return FALSE;
testlist = llParseString2List(test,["@"],[]); // check for just one '@'
if ( llGetListLength(testlist) < 2 ) return FALSE;
if ( llGetListLength(testlist) > 2 ) return FALSE;
list testlist2 = llParseString2List((string)llList2List(testlist,1,1),["."],[]); // check for '.'
if ( llGetListLength(testlist2) < 2 ) return FALSE;
else return TRUE;
}

Then, anywhere in the code I wish to test whether a string is an e-mail address, I call the function with: "if ( IsEmailAddr(message) )" (in this case, the variable 'message' is the message returned by a listen; I'm checking whether what a user types into chat is an e-mail address). The code following this if statement only executes if the IsEmailAddr function returns TRUE.

How it works:

First, I check whether the string contains any spaces. If it contains spaces, then it's not an e-mail address.

Then, I check to see if the string contains a '@' and there is text on either side of the '@' by using the '@' to parse the list. If this returns a list with two elements, then I know there's one @ somewhere in the middle of the string. If testlist has only one element, or more than two elements, then it has more than one '@' and could not be a valid e-mail address.

Next, I check the part that appears after the '@' to see if it contains at least one '.'. If it does, then it looks like it's got the right formatting elements to be accepted by an e-mail server.

I realize there are some string combinations that might return false positives and that there's no way to check if an address belongs to a valid account until e-mail is actually sent, but, this is the best I can come up with. I'm sure a gazillion ways to check whether input is an e-mail address have cropped up over the years; what's best for use in SL?
Kurzweil Sleeper
Registered User
Join date: 4 Jan 2006
Posts: 12
now I haven't tried this...
08-28-2007 18:08
but this was my thought. if you have a server, you can run a whois query to make sure the domain is registered. Perhaps there is even a way to query a registry from within SL. I do not have the skillz...



integer IsEmailAddr(string test)
{
//I call upon the string sorting power of llSubStringIndex()
//which may or may not be a cheaper operation than sorting to lists.
//all i do here is make sure that first there are no spaces, and
// then that there is an "@" followed by a "."
if (llSubStringIndex(test, " ";) != -1){return FALSE;}
integer whereatis = llSubStringIndex(test, "@";);
integer wheredotis = llSubStringIndex(test, ".";);
integer minimumatvalue = -1; // you can up this number if
// you want to assume minimum length of address.
else if (whereatis < wheredotis && whereatis > minimumatvalue){return TRUE;}
}
Little Gray
Registered User
Join date: 16 Oct 2006
Posts: 48
08-28-2007 19:32
hrm .. llSubStringIndex .. that's the kind of command I was looking for .. probably faster to execute and makes the code simpler. will test & compare. however 'wheredotis < whereatis' will fail if an e-mail address contains a dot in the first part, as in [email]Jane.Doe@mydomain.com[/email] as some e-mail addresses have.
Kenn Nilsson
AeonVox
Join date: 24 May 2005
Posts: 897
08-28-2007 19:39
You may want to strengthen the function by making sure that the remaining string length after the . is 2-3 characters only.
_____________________
--AeonVox--

Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms chasing ghosts, eating magic pills, and listening to repetitive, addictive, electronic music.
Kurzweil Sleeper
Registered User
Join date: 4 Jan 2006
Posts: 12
08-29-2007 16:51
my own email addresses have [dot]s before [@]s! Why didn't I think of that. I ph33r I never will be truly l33t. Good call on the 2-3chars after the [.]

[email]kurzweil.sleeper@gmail.com[/email]
Siann Beck
Beauty & Braiiiiinsss!
Join date: 14 Jul 2007
Posts: 140
08-30-2007 05:50
I would add TLD checking. Go ahead and do the dot split; the last element then must be a valid TLD or country code. If it's a country code, the element before it must be a valid country TLD (e.g. [email]address@domain.co.uk[/email])

list lstGeneralTLDs = [
"com", "net", "org", "edu", "gov", "mil", "int",
"arpa", "aero", "biz", "coop", "info", "museum",
"name"
];

list lstCountryCodes = [
"ad", "ae", "af", "ag", "ai", "al", "am", "an", "ao", "aq",
"ar", "as", "at", "au", "aw", "az", "ba", "bb", "bd", "be",
"bf", "bg", "bh", "bi", "bj", "bm", "bn", "bo", "br", "bs",
"bt", "bv", "bw", "by", "bz", "ca", "cc", "cf", "cd", "cg",
"ch", "ci", "ck", "cl", "cm", "cn", "co", "cr", "cs", "cu",
"cv", "cx", "cy", "cz", "de", "dj", "dk", "dm", "do", "dz",
"ec", "ee", "eg", "eh", "er", "es", "et", "fi", "fj", "fk",
"fm", "fo", "fr", "fx", "ga", "gb", "gd", "ge", "gf", "gh",
"gi", "gl", "gm", "gn", "gp", "gq", "gr", "gs", "gt", "gu",
"gw", "gy", "hk", "hm", "hn", "hr", "ht", "hu", "id", "ie",
"il", "in", "io", "iq", "ir", "is", "it", "jm", "jo", "jp",
"ke", "kg", "kh", "ki", "km", "kn", "kp", "kr", "kw", "ky",
"kz", "la", "lb", "lc", "li", "lk", "lr", "ls", "lt", "lu",
"lv", "ly", "ma", "mc", "md", "mg", "mh", "mk", "ml", "mm",
"mn", "mo", "mp", "mq", "mr", "ms", "mt", "mu", "mv", "mw",
"mx", "my", "mz", "na", "nc", "ne", "nf", "ng", "ni", "nl",
"no", "np", "nr", "nt", "nu", "nz", "om", "pa", "pe", "pf",
"pg", "ph", "pk", "pl", "pm", "pn", "pr", "pt", "pw", "py",
"qa", "re", "ro", "ru", "rw", "sa", "sb", "sc", "sd", "se",
"sg", "sh", "si", "sj", "sk", "sl", "sm", "sn", "so", "sr",
"st", "su", "sv", "sy", "sz", "tc", "td", "tf", "tg", "th",
"tj", "tk", "tm", "tn", "to", "tp", "tr", "tt", "tv", "tw",
"tz", "ua", "ug", "uk", "um", "us", "uy", "uz", "va", "vc",
"ve", "vg", "vi", "vn", "vu", "wf", "ws", "ye", "yt", "yu",
"za", "zm", "zr", "zw"
];

list lstCountryTLDs = [
"com", "net", "org", "edu", "gov", "mil", "co", "ne", "or",
"ed", "go", "mi", "aero", "biz", "coop", "info", "museum",
"name"
];