Check an E-mail Address is Valid

Home

Sometimes you need to check that an e-mail address entered in a form is valid. This simple function checks that the e-mail address has an @ symbol, something before the @, and someting with a . in it after the @. It's not foolproof but it'll make sure people at least attempt to get their e-mail address right.

The function accepts a string, and returns true or false.

function checkEmail(email) {
  if (email.length == 0) { return true }
  var at = email.indexOf("@");
  var dot = email.lastIndexOf(".");
  if (email == "") { return false }
  if (at < 1) { return false }
  if (dot < at + 2) { return false }
  if (dot > (email.length - 3)) { return false }
  return true;
}