March 10, 2000 - Validating email Address | WebReference

March 10, 2000 - Validating email Address

Yehuda Shiran March 10, 2000
Validating email Addresses
Tips: March 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

Often times you need to validate the e-mail address of your addressees, just before they want to send you a message. Here are the logical checks:

Here is a function that validates this sequence:

function valid(form) {
  var field = form.email; // email field
  var str = field.value; // email string
  var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
  var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
  if (!reg1.test(str) && reg2.test(str)) { // if syntax is valid
    alert("Thank your for your feedback."); // this is optional
    return true;
  }
  alert("\"" + str + "\" is an invalid e-mail!"); // this is also optional
  field.focus();
  field.select();
  return false;
}

Learn more about our validation script in Column 5, JavaScript Regular Expressions.