Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression for Email validation which detect ".com.com" [duplicate]

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...

like image 529
Siddharth Avatar asked May 27 '26 15:05

Siddharth


2 Answers

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})$

like image 131
robinCTS Avatar answered May 30 '26 04:05

robinCTS


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;
}
like image 40
Alessandro Minoccheri Avatar answered May 30 '26 04:05

Alessandro Minoccheri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!