PDA

View Full Version : E-Mail Validation using JavaScript



Pravin Dingore
May 12th, 2006, 10:54 AM
Hello all
This is the first time I am submitting my code in forum and
I am very happy doing so becuase I am fighting with email validation from last day and finally I have done it.
Please fill free to use this function for email validation and save you time

function isEmail(str)
{
/* uncomment this if e-mail is not mondatory
if(str.length<=0)
return 1; */
var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
var objRegExp = /(^[a-z]([a-z0-9_\.]*)@([^-][a-z0-9-_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z0-9_\.]*)@([^-][a-z0-9-_\.]*)(\.[a-z]{2,3})(\.[a-z]{2})*$)/i;
return (!r1.test(str) && objRegExp.test(str));
}

:thumbsup:

felgall
May 12th, 2006, 11:15 PM
Any email addresses in the top level 'museum' domain will fail this test because you only accept 2 and 3 character top level domains. Similarly for 'aero', 'coop', 'name', and 'info' top level domains.

Pravin Dingore
May 15th, 2006, 08:07 AM
thanks for showing the limitation here is updated function

function isEmail(str)
{
/* uncomment this if e-mail is not mondatory
if(str.length<=0)
return 1; */
var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
var objRegExp = /(^[a-z]([a-z0-9_\.]*)@([a-z0-9][a-z0-9-_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z0-9_\.]*)@([a-z0-9][a-z0-9-_\.]*)(\.[a-z]{2,4})(\.[a-z]{2})*$)/i;
return (!r1.test(str) && objRegExp.test(str));
}

Philip M
May 15th, 2006, 05:30 PM
More wheels of varying shapes (some of them round ;) ) may be found at

http://regexlib.com/Search.aspx?k=email