1

Possible Duplicate:
Validate email address in Javascript?

I'm having some trouble with validating email address client side using Javascript. After a lot of searching I found a regular expression which is as follows:-

/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$ 

It was working fine with most email ids I have tested with. But suddenly I discovered that it's also saying email's with .com.com like extensions, which is a serious bug for me.

How my question is that how I can modify the mentioned regular expression which will invalid .com.com also.

I should also mention that i have also tried with many other regular expressions but they all seem to have this same mentioned problem...

Thanks...

Community
  • 1
  • 1
Siddharth
  • 833
  • 6
  • 12
  • 25
  • 2
    *why* is this valid extension a problem for you? – JaredC Jan 20 '13 at 17:13
  • 1
    http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address – Andreas Jan 20 '13 at 17:15
  • http://ex-parrot.com/~pdw/Mail-RFC822-Address.html – Brad Jan 20 '13 at 17:15
  • 1
    `com.com` is a real domain name. People could have email addresses there. – Quentin Jan 20 '13 at 17:18
  • 1
    Please don't use a regex that excludes using a + in the first portion of the email address. Fluent validation uses this regex and I haven't run into any problems with using it in production: http://regexlib.com/REDetails.aspx?regexp_id=1448&AspxAutoDetectCookieSupport=1 – Justin Edwards Jan 20 '13 at 17:19

3 Answers3

1

Try this regex:

function verifyEmail(){
var status = false;     
var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
     if (document.myform.email1.value.search(emailRegEx) == -1) {
          alert("Please enter a valid email address.");
     }
     else{
         status = true;
     }
     return status;
}
Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171
1

This will invalidate any address ending in .com.com:

/^([A-Za-z0-9_\-\.])+\@(?![A-Za-z0-9_\-\.]+\.com\.com)([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$

This will also invalidate those ending in @com.com:

/^([A-Za-z0-9_\-\.])+\@(?!(?:[A-Za-z0-9_\-\.]+\.)?com\.com)([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$

Finally, this will invalidate those ending in any repetition, eg, .net.net, .au.au, etc:

/^([A-Za-z0-9_\-\.])+\@(?!(?:[A-Za-z0-9_\-\.]+\.)?([A-Za-z]{2,4})\.\2)([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$

robinCTS
  • 5,746
  • 14
  • 30
  • 37
0
function emailCheck (emailStr) {
 var checkTLD=1;
   var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
  var emailPat=/^(.+)@(.+)$/;
  var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
  var validChars="\[^\\s" + specialChars + "\]";
  var quotedUser="(\"[^\"]*\")";
  var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
  var atom=validChars + '+';

  var word="(" + atom + "|" + quotedUser + ")";
  var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
  var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
 var matchArray=emailStr.match(emailPat);
  if (matchArray==null) {
  alert("Email address seems incorrect (check @ and .'s)");
return false;
}

The official email check method of my ISP website. No copyright on this. So you can use it.

Arpit
  • 12,767
  • 3
  • 27
  • 40