function isValidEmail(S) { return /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(S); } use it like this if( !isValidEmail(document.getElementById('someformelement').value) ) { alert('invalid email'); }
Take note, serialCoder's code won't properly validate all e-mail addresses. Especially those containing "+" in the part of an e-mail address string before "@" delimiter. Most of the websites and e-mail providers do not even know about "+" is normally allowed there and mark it as invalid character, which is wrong and is against RFC. Also, it won't validate e-mail addresses with TLD longer than 3 characters, which is also completely wrong, because there are TLD's longer than 3 characters, such as "info" or "mobi". So, if the website you need this for is properly coded and your SMTP is not violating RFC, you should use at least the RegExp string I provide below. Anyway, I'd recommend you to create or pay someone to create more robust solution for you. return /^\w+[\+\.\w-]*@\w+([\.-]?\w+)*(\.\w{2,6})+$/.test(S); Code (markup):
darn, they allow + in the email addresses?? well, anyways its something i just typed up thanks for pointing it out