Validating an e-mail - example
function isValid(email) {
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(String(email).toLowerCase());
}
console.log(isValid('iamvalid@gmail.com');
console.log(isValid('iamnotvalid@gmail');
If you run this code in your browser, the first e-mail will return true and the second one false.
The code if kind of easy to understand, but the regex not so much.
We simply use a regex to check of the email starts with a certain type of string, if it has a "@", if it has a domain and that the domain has an extension.