View Full Version : Validating Email ?
priyakochin
Jul 4th 2008, 3:43 am
Can anyone help me with the code to validate email ID ?
serialCoder
Jul 4th 2008, 4:33 am
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');
}
=Messa=
Jul 4th 2008, 3:03 pm
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);
serialCoder
Jul 4th 2008, 3:55 pm
darn, they allow + in the email addresses??
well, anyways its something i just typed up :)
thanks for pointing it out
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);
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.