Regex Email validation

后端 未结 30 1223
北海茫月
北海茫月 2020-11-22 12:16

I use this

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

regexp to validate the email

([\\w\\.\\-]+) - this is f

30条回答
  •  没有蜡笔的小新
    2020-11-22 12:35

    I found nice document on MSDN for it.

    How to: Verify that Strings Are in Valid Email Format http://msdn.microsoft.com/en-us/library/01escwtf.aspx (check out that this code also supports the use of non-ASCII characters for Internet domain names.)

    There are 2 implementation, for .Net 2.0/3.0 and for .Net 3.5 and higher.
    the 2.0/3.0 version is:

    bool IsValidEmail(string strIn)
    {
        // Return true if strIn is in valid e-mail format.
        return Regex.IsMatch(strIn, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"); 
    }
    

    My tests over this method give:

    Invalid: @majjf.com
    Invalid: A@b@c@example.com
    Invalid: Abc.example.com
    Valid: j..s@proseware.com
    Valid: j.@server1.proseware.com
    Invalid: js*@proseware.com
    Invalid: js@proseware..com
    Valid: ma...ma@jjf.co
    Valid: ma.@jjf.com
    Invalid: ma@@jjf.com
    Invalid: ma@jjf.
    Invalid: ma@jjf..com
    Invalid: ma@jjf.c
    Invalid: ma_@jjf
    Invalid: ma_@jjf.
    Valid: ma_@jjf.com
    Invalid: -------
    Valid: 12@hostname.com
    Valid: d.j@server1.proseware.com
    Valid: david.jones@proseware.com
    Valid: j.s@server1.proseware.com
    Invalid: j@proseware.com9
    Valid: j_9@[129.126.118.1]
    Valid: jones@ms1.proseware.com
    Invalid: js#internal@proseware.com
    Invalid: js@proseware.com9
    Invalid: js@proseware.com9
    Valid: m.a@hostname.co
    Valid: m_a1a@hostname.com
    Valid: ma.h.saraf.onemore@hostname.com.edu
    Valid: ma@hostname.com
    Invalid: ma@hostname.comcom
    Invalid: MA@hostname.coMCom
    Valid: ma12@hostname.com
    Valid: ma-a.aa@hostname.com.edu
    Valid: ma-a@hostname.com
    Valid: ma-a@hostname.com.edu
    Valid: ma-a@1hostname.com
    Valid: ma.a@1hostname.com
    Valid: ma@1hostname.com
    

提交回复
热议问题