-4

I need to validate email address entered into a input textbox using regex.

How can I use regex \b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b to compare with the entered email id?

Pls suggest.

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
prakash
  • 131
  • 1
  • 15

4 Answers4

1

Email-Address validation is a never ending story. Even if you could manage to find a pattern, that does match valid addresses, that doesn't mean, that the address is really valid, and the receiver is existing.

Especially with the new upcoming top-level-domains the commonly used [A-Z]{2,4} at the end will be obsolete.(i.e. somebody@company.technology) Also some companies use email-addresses depending on subdomains (i.e. somebody@division.company.tld), which would produce multiple dots in the domain.

Therefore the ONLY valid check on email adresses would be to look for the presence of an @ sign and maybe non-empty strings around it - and invalid characters.

dognose
  • 20,360
  • 9
  • 61
  • 107
0

Try this

function validateEmail(email) { 
    var re = /\b[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}\b/;
    return re.test(email);
} 

var email = document.getElementById("email").value;

if ( validateEmail(email) ) {
    alert('valid');
} else {
    alert('invalid');
}
zzlalani
  • 22,960
  • 16
  • 44
  • 73
0
function echeck(){


        var at="@"
        var dot="."
        var str=document.registerapplicant.regtxtemail.value;
        var lat=str.indexOf(at)
        var lstr=str.length
        var ldot=str.indexOf(dot)
        if(str=='')
        {
        return false
        }
        if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
        {
           alert("Invalid E-mail ID")
           document.registerapplicant.regtxtemail.value='';
           document.registerapplicant.regtxtemail.focus();

           return false
        }

        if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
        {
            alert("Invalid E-mail ID")
            document.registerapplicant.regtxtemail.value='';
            document.registerapplicant.regtxtemail.focus();
            return false
        }

         if (str.indexOf(at,(lat+1))!=-1)
         {
            alert("Invalid E-mail ID")
            document.registerapplicant.regtxtemail.value='';
            document.registerapplicant.regtxtemail.focus();
            return false
         }

         if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
         {
            alert("Invalid E-mail ID")
            document.registerapplicant.regtxtemail.value='';
            document.registerapplicant.regtxtemail.focus();
            return false
         }

         if (str.indexOf(dot,(lat+2))==-1)
         {
            alert("Invalid E-mail ID")
            document.registerapplicant.regtxtemail.value='';
            document.registerapplicant.regtxtemail.focus();
            return false
         }

         if (str.indexOf(" ")!=-1){
            alert("Invalid E-mail ID")
            document.registerapplicant.regtxtemail.value='';
            document.registerapplicant.regtxtemail.focus();
            return false
         }

         //return true

    }   

this works smooth and fine

Sujith PS
  • 4,776
  • 3
  • 34
  • 61
  • It is not mandatory to have a dot in an email address. See http://en.wikipedia.org/wiki/Email_address#Valid_email_addresses – Toto Feb 03 '14 at 12:42
0

According to the reference pointed out to me in the comments, this should work -

/*
    I have split the patterns into three different regexs.
*/
function email_valid(str){
    var patt1=/(?:[a-zA-Z][a-zA-Z0-9\+-]*(?:[._][a-zA-Z0-9\+-]+)*)\@[a-zA-Z0-9]+(?:\.[a-zA-Z]+)*/g;
    var patt2 = /(?:\".*?\")\@[a-zA-Z]+(?:\.[a-zA-Z]+)*/g;
    var patt3 = /(?:[a-zA-Z][a-zA-Z0-9\+-]*(?:[._][a-zA-Z0-9\+-]+)*)\@\[IPv6:[0-9a-f]+:[0-9a-f]*:[0-9a-f]*:[0-9a-f]*:[0-9a-f]*:[0-9a-f]+\]/g;
    if(patt1.exec(str) == str){return true;}
    if(patt2.exec(str) == str){return true;}
    if(patt3.exec(str) == str){return true;}
    return false;
}

/*
This is the output from the reference given. It is not a 100%, but still good enough.

Correct Email-Ids, Answer must be true

niceandsimple@example.com                                               => true
very.common@example.com                                                 => true
a.little.lengthy.but.fine@dept.example.com                              => true
disposable.style.email.with+symbol@example.com                          => true
other.email-with-dash@example.com                                       => true
user@[IPv6:2001:db8:1ff::a0b:dbd0]                                      => true
"much.more unusual"@example.com                                         => true
"very.unusual.@.unusual.com"@example.com                                => true
"very.(),:;<>[]".VERY."very@\ "very".unusual"@strange.example.com       => true         
postbox@com                                                             => true
admin@mailserver1                                                       => true
!#$%&'*+-/=?^_`{}|~@example.org                                           => false
"()<>[]:,;@\"!#$%&'*+-/=?^_`{}| ~.a"@example.org                          => true
" "@example.org                                                         => true
üñîçøðé@example.com                                                     => false
üñîçøðé@üñîçøðé.com                                                     => false

Incorrect Email-Ids, Answer must be false

Abc.example.com                                                         => false
A@b@c@example.com                                                       => false
a"b(c)d,e:f;gi[j\k]l@example.com                                        => false
just"not"right@example.com                                              => false
this is"notllowed@example.com                                        => false
this\ still"not\allowed@example.com                                     => false
*/
Kamehameha
  • 5,423
  • 1
  • 23
  • 28