Posts

Showing posts from August, 2016

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 add…