-1

I need to check whether given email address is invalid in action script. Following is the code/regex i came up with.

private function isEmailInvalid(email:String):Boolean
            {
                var pattern:RegExp = /(\w|[_.\-])+@((\w|-)+\.)+\w{2,4}+/;
                var result:Object = pattern.exec(email);
                if(result == null) {
                    return true;
                }
                return false;
            }

But it seems like above code do not cover all the test cases in the following link: http://blogs.msdn.com/b/testing123/archive/2009/02/05/email-address-test-cases.aspx

Does anyone have better way of doing this?

Folowing are the tested valid emails i used (above function should return "false" for these):

firstname.lastname@domain.com 
firstname+lastname@domain.com 
email@domain.co.jp 

Folowing are the invalid ones (so function should return "true" for these):

email@domain@domain.com
.email@domain.com
email..email@domain.com
plainaddress, email@domain..com
ketan
  • 19,129
  • 42
  • 60
  • 98
adobeFlex
  • 1
  • 3
  • 1
    `\w{2,4}` is wrong for matching TLD. It matches `___` and doesn't match `museum`. Here is the official list of TLDs at this time: https://data.iana.org/TLD/tlds-alpha-by-domain.txt – Toto Oct 28 '14 at 08:32
  • 1
    Don't use regex to validate email addresses. They invariably exclude many perfectly valid email addresses, causing unnecessary frustration to your users. – Brian Oct 29 '14 at 23:17
  • like to know, what'll be the best way to validate email addresses other than using RegEx? – adobeFlex Oct 30 '14 at 05:39

2 Answers2

0

Remove the + at the last and you must need to put anchors.

^(\w|[_.\-])+@((\w|-)+\.)+\w{2,4}$

Simplified one,

^[\w_.-]+@([\w-]+\.)+\w{2,4}$

DEMO

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • Thanks Avinash. Will give this a try and let u know. – adobeFlex Oct 28 '14 at 07:17
  • @AvinashRaj But have you checked the link OP has given? it a long list. Am not sure how to match the entire – nu11p01n73R Oct 28 '14 at 07:28
  • Seems like it returning "true" for both cases: (email@domain.com) is invalid: true | (plainaddress) is invalid: true (tested email address is given inside the bracket) – adobeFlex Oct 28 '14 at 07:31
  • post the tested email addresses in your question. – Avinash Raj Oct 28 '14 at 07:38
  • [Tested valid emails: firstname.lastname@domain.com, firstname+lastname@domain.com, email@domain.co.jp ] [Tested invalid emails: email@domain@domain.com, .email@domain.com, email..email@domain.com, plainaddress, email@domain..com] – adobeFlex Oct 28 '14 at 09:22
  • Both of these expressions are wrong. They will incorrectly mark many valid emails as "invalid". See http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx/ and http://regexr.com/39qmq for a demo – Brian Oct 29 '14 at 23:08
0

Try this RegExp :

RegExp = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
Mayur Nandane
  • 309
  • 2
  • 8