I've slightly modified Jaymon's answer for people who want really simple validation in the form of:
[email protected]
The regular expression:
/\S+@\S+\^\S+@\S+\.\S+\S+$/
To prevent matching multiple @ signs:
/^[^\s@]+@[^\s@]+\.[^\s@]+$/
The above regexes match the whole string, remove the leading and ^
and trailing $
if you want to match anywhere in the string. The example below matches anywhere in the string.
If you do want to match the whole sring, you may want to trim()
the string first.
Example JavaScript function:
function validateEmail(email)
{
var re = /\S+@\S+\.\S+/;
return re.test(email);
}
}
console.log(validateEmail('my email is [email protected]')); // true
console.log(validateEmail('anystring@anystring'my email is anystring@anystring .anystring'any')); // false