Validate Email Address [JavaScript]


The following is a quick and dirty way to validate form input, using JavaScript, from a user to confirm that they are entering a value, which could be interpreted as an email address. The function below is meant to quickly and simply ensure that the value provided by the user contains the at '@' symbol and a period '.', nothing more!

// Vanilla JavaScript
function validateEmail(emailAddress) {
  if (emailAddress.value.indexOf('@') == -1 || emailAddress.value.indexOf('.') == -1) {
    alert("Invalid Email!");
    document.getElementById(emailAddress.id).focus();

    return false;
  }
}

The validateEmila function relies on the global method indexOf. That method returns the first position at which the specified string value occurs.

Keep in mind that this is not a complete and exhaustive method of validating an email address. There are better and more comprehensive ways to do so. For example, make sure that the email address doesn’t contain any spaces, symbols, HTML characters, or that it starts with a letter, not a digit.


Discover more from Titan Fusion

Subscribe to get the latest posts sent to your email.


Discover more from Titan Fusion

Subscribe now to keep reading and get access to the full archive.

Continue reading