I’ve seen some pretty horrible code in my days of coding, but nothing beats this:
_global.checkEmail = function(e) {
function checkChars(s, i, l) {
while (i < l && "_-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".indexOf(s.charAt(i)) != -1)
{
i = ++i;
} // end while
return (i);
}
function checkFirstLevelDomainChars(s, i, l)
{
while (i < l && "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".indexOf(s.charAt(j)) != -1)
{
i = ++i;
} // end while
return (i == l);
} // End of the function
var _loc1;
var j;
var _loc2 = e.length;
var _loc4 = false;
_loc1 = checkChars(e, 0, _loc2);
if (checkChars(e, 0, _loc2) == 0)
{
return (-1);
} // end if
j = _loc1;
while (_loc1 < _loc2 && e.charAt(_loc1) == ".")
{
++_loc1;
if ((j = checkChars(e, _loc1, _loc2)) == _loc1)
{
return (-2);
} // end if
_loc1 = j;
} // end while
if (e.charAt(_loc1) != "@")
{
return (-3);
} // end if
do
{
_loc1 = j + 1;
j = checkChars(e, _loc1, _loc2);
if (j == _loc1)
{
return (-4);
}
else if (j == e.length)
{
j = j - _loc1;
if (_loc4 && j >= 2 && checkFirstLevelDomainChars(e, _loc1, _loc2))
{
return (1);
}
else
{
return (-5);
} // end else if
} // end else if
_loc4 = e.charAt(j) == ".";
} while (_loc1 < _loc2 && _loc4)
return (-6);
};
function checkChars(s, i, l) {
while (i < l && "_-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".indexOf(s.charAt(i)) != -1)
{
i = ++i;
} // end while
return (i);
}
function checkFirstLevelDomainChars(s, i, l)
{
while (i < l && "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".indexOf(s.charAt(j)) != -1)
{
i = ++i;
} // end while
return (i == l);
} // End of the function
var _loc1;
var j;
var _loc2 = e.length;
var _loc4 = false;
_loc1 = checkChars(e, 0, _loc2);
if (checkChars(e, 0, _loc2) == 0)
{
return (-1);
} // end if
j = _loc1;
while (_loc1 < _loc2 && e.charAt(_loc1) == ".")
{
++_loc1;
if ((j = checkChars(e, _loc1, _loc2)) == _loc1)
{
return (-2);
} // end if
_loc1 = j;
} // end while
if (e.charAt(_loc1) != "@")
{
return (-3);
} // end if
do
{
_loc1 = j + 1;
j = checkChars(e, _loc1, _loc2);
if (j == _loc1)
{
return (-4);
}
else if (j == e.length)
{
j = j - _loc1;
if (_loc4 && j >= 2 && checkFirstLevelDomainChars(e, _loc1, _loc2))
{
return (1);
}
else
{
return (-5);
} // end else if
} // end else if
_loc4 = e.charAt(j) == ".";
} while (_loc1 < _loc2 && _loc4)
return (-6);
};
I’m really not sure what that programmer what smoking when he created that mess; and I really don’t want to know. It felt really good removing 65 lines of code and replacing it with this though:
_global.checkEmail = function(email) { return email.length != 0 && email.indexOf("@") != -1 && email.indexOf(".") != -1; }