Skip to content

Instantly share code, notes, and snippets.

@KrofDrakula
Created January 25, 2012 07:37
Show Gist options
  • Save KrofDrakula/1675236 to your computer and use it in GitHub Desktop.
Save KrofDrakula/1675236 to your computer and use it in GitHub Desktop.
Email validation? Seriously?
//
// Email address must be of form [email protected] ... in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
function isValidEmail(s)
{
if (isEmpty(s)) return false;
// is s whitespace?
if (isWhitespace(s)) return false;
// there must be >= 1 character before @, so we
// start looking at character position 1
// (i.e. second character)
var i = 1;
var sLength = s.length;
// look for @
while ((i < sLength) && (s.charAt(i) != "@"))
{ i++
}
if ((i >= sLength) || (s.charAt(i) != "@")) return false;
else i += 2;
// look for .
while ((i < sLength) && (s.charAt(i) != "."))
{ i++
}
// there must be at least one character after the .
if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
else return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment