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?
!= -1){return FALSE;}