0

Possible Duplicate:
Validate email address in Javascript?

I have javascript for email validation :

     //function to check emails
     function echeck(str) {
   var at="@"
   var dot="."
   var lat=str.indexOf(at)
   var lstr=str.length
   var ldot=str.indexOf(dot)
   if (str.indexOf(at)==-1){
     return false;
   }

   if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
   return false;
   }

   if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
    return false;   }

    if (str.indexOf(at,(lat+1))!=-1){
    return false; }

    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
    return false; }

    if (str.indexOf(dot,(lat+2))==-1){
    return false;    }

    if (str.indexOf(" ")!=-1){
    return false;
   }

    return true;                    
    }

It working fine but not checking this one -> !!!!!!!!!@####.com

Kindly help me to check extra spl characters in email.

Regards,

Community
  • 1
  • 1
user264341
  • 137
  • 1
  • 6
  • 14
  • Is the answer you seek not [here](http://stackoverflow.com/questions/46155/validate-email-address-in-javascript)? Beaten by @Mark Biek by 19 seconds :-( – Xav Aug 17 '11 at 15:09
  • Screw validation. With all sorts of weird TLDs and non-ASCII character sets in domains nowadays, any validation beyond `*@*` is completely useless – Pekka Aug 17 '11 at 15:22

1 Answers1

2

The example you provided is not a valid email address at least because ####.com is not a valid domain name. What you really should do is use regular expression to check the email address.

The most simple version of the regex would be

/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/

and a much more complete one would be

/^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum|xxx)$/

and the official RFC2822 implementation would be

/^(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])$/

And you would actually check the email address like so:

function echeck(str)
{
    //using the most simple version
    //substitute for another pattern if more complete matching is required
    var expr = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
    return expr.test(str);
}

Note that it's not a good idea to just do validation in JS. You should always perform another validation in the server - to prevent somebody passing bad data into your script and you happily using it. Keep in mind that you can't guarantee that the form will be submitted from your page.

Aleks G
  • 56,435
  • 29
  • 168
  • 265