2

Possible Duplicate:
Validate email address in Javascript?

Can anyone help me with email validation in javascript?.

Here's the script:

Function validateEmail(email)
{
    var splitted = email.match("^(.+)@(.+)$");
    if (splitted == null) return false;
    if (splitted[1] != null)
    {
        var regexp_user = /^\"?[\w-_\.]*\"?$/;
        if (splitted[1].match(regexp_user) == null) return false;
    }
    if (splitted[2] != null)
    {
        var regexp_domain = /^[\w-\.]*\.[A-Za-z]{2,4}$/;
        if (splitted[2].match(regexp_domain) == null)
        {
            var regexp_ip = /^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
            if (splitted[2].match(regexp_ip) == null) return false;
        } // if
        return true;
    }
    return false;
}

it runs fine but when someone enters "rohit@.com" as a email it takes that also rather it should be showing an error.

Can anyone help me on this to fix it? (Sorry I am a newbie to this so ignore any faults)

Community
  • 1
  • 1
Rohit Batra
  • 674
  • 4
  • 18

1 Answers1

1
function validateEmail(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(email);
} 

Source : Validate email address in JavaScript?

Community
  • 1
  • 1
Mohit Bumb
  • 2,466
  • 5
  • 33
  • 52