How do I validate email address formatting with the .NET Framework?

前端 未结 11 607
孤城傲影
孤城傲影 2020-11-29 05:49

I want a function to test that a string is formatted like an email address.

What comes built-in with the .NET framework to do this?

This works:



        
相关标签:
11条回答
  • 2020-11-29 06:24

    Another function to check that the email is valid or not :

    Public Function ValidEmail(ByVal strCheck As String) As Boolean
        Try
            Dim bCK As Boolean
            Dim strDomainType As String
            Const sInvalidChars As String = "!#$%^&*()=+{}[]|\;:'/?>,< "
            Dim i As Integer
            'Check to see if there is a double quote
            bCK = Not InStr(1, strCheck, Chr(34)) > 0
            If Not bCK Then GoTo ExitFunction
            'Check to see if there are consecutive dots
            bCK = Not InStr(1, strCheck, "..") > 0
            If Not bCK Then GoTo ExitFunction
            ' Check for invalid characters.
            If Len(strCheck) > Len(sInvalidChars) Then
                For i = 1 To Len(sInvalidChars)
                    If InStr(strCheck, Mid(sInvalidChars, i, 1)) > 0 Then
                        bCK = False
                        GoTo ExitFunction
                    End If
                Next
            Else
                For i = 1 To Len(strCheck)
                    If InStr(sInvalidChars, Mid(strCheck, i, 1)) > 0 Then
                        bCK = False
                        GoTo ExitFunction
                    End If
                Next
            End If
    
            If InStr(1, strCheck, "@") > 1 Then 'Check for an @ symbol
                bCK = Len(Left(strCheck, InStr(1, strCheck, "@") - 1)) > 0
            Else
                bCK = False
            End If
            If Not bCK Then GoTo ExitFunction
            strCheck = Right(strCheck, Len(strCheck) - InStr(1, strCheck, "@"))
            bCK = Not InStr(1, strCheck, "@") > 0 'Check to see if there are too many @'s
            If Not bCK Then GoTo ExitFunction
            strDomainType = Right(strCheck, Len(strCheck) - InStr(1, strCheck, "."))
            bCK = Len(strDomainType) > 0 And InStr(1, strCheck, ".") < Len(strCheck)
            If Not bCK Then GoTo ExitFunction
            strCheck = Left(strCheck, Len(strCheck) - Len(strDomainType) - 1)
            Do Until InStr(1, strCheck, ".") <= 1
                If Len(strCheck) >= InStr(1, strCheck, ".") Then
                    strCheck = Left(strCheck, Len(strCheck) - (InStr(1, strCheck, ".") - 1))
                Else
                    bCK = False
                    GoTo ExitFunction
                End If
            Loop
            If strCheck = "." Or Len(strCheck) = 0 Then bCK = False
    ExitFunction:
            ValidEmail = bCK
        Catch ex As ArgumentException
            Return False
        End Try
        Return ValidEmail
    End Function
    

    How to use it:

    Private Sub TextBox2_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox2.KeyDown
        If e.KeyCode = Keys.Enter Then
            If TextBox2.Text = "" Then
                MsgBox("Write Down Your email and Press Enter") : TextBox2.Select()
            Else
    
                If ValidEmail(TextBox2.Text) Then ' to check if the email is valid or not
                       'do whatever
                Else
                    MsgBox("Please Write Valid Email")
                    TextBox2.Select()
                End If
            End If
        End If
    End Sub
    
    0 讨论(0)
  • 2020-11-29 06:27
    '-----------------------------------------------------------------------
    
    'Creater : Rachitha Madusanka
    
    'http://www.megazoon.com
    
    'jewandara@gmail.com
    
    'jewandara@hotmail.com
    
    'Web Designer and Software Developer
    
    '@ http://www.zionx.net16.net
    
    '-----------------------------------------------------------------------
    
    
    
    
    Function ValidEmail(ByVal strCheck As String) As Boolean
        Try
            Dim bCK As Boolean
            Dim strDomainType As String
    
    
            Const sInvalidChars As String = "!#$%^&*()=+{}[]|\;:'/?>,< "
            Dim i As Integer
    
            'Check to see if there is a double quote
            bCK = Not InStr(1, strCheck, Chr(34)) > 0
            If Not bCK Then GoTo ExitFunction
    
            'Check to see if there are consecutive dots
            bCK = Not InStr(1, strCheck, "..") > 0
            If Not bCK Then GoTo ExitFunction
    
            ' Check for invalid characters.
            If Len(strCheck) > Len(sInvalidChars) Then
                For i = 1 To Len(sInvalidChars)
                    If InStr(strCheck, Mid(sInvalidChars, i, 1)) > 0 Then
                        bCK = False
                        GoTo ExitFunction
                    End If
                Next
            Else
                For i = 1 To Len(strCheck)
                    If InStr(sInvalidChars, Mid(strCheck, i, 1)) > 0 Then
                        bCK = False
                        GoTo ExitFunction
                    End If
                Next
            End If
    
            If InStr(1, strCheck, "@") > 1 Then 'Check for an @ symbol
                bCK = Len(Left(strCheck, InStr(1, strCheck, "@") - 1)) > 0
            Else
                bCK = False
            End If
            If Not bCK Then GoTo ExitFunction
    
            strCheck = Right(strCheck, Len(strCheck) - InStr(1, strCheck, "@"))
            bCK = Not InStr(1, strCheck, "@") > 0 'Check to see if there are too many @'s
            If Not bCK Then GoTo ExitFunction
    
            strDomainType = Right(strCheck, Len(strCheck) - InStr(1, strCheck, "."))
            bCK = Len(strDomainType) > 0 And InStr(1, strCheck, ".") < Len(strCheck)
            If Not bCK Then GoTo ExitFunction
    
            strCheck = Left(strCheck, Len(strCheck) - Len(strDomainType) - 1)
            Do Until InStr(1, strCheck, ".") <= 1
                If Len(strCheck) >= InStr(1, strCheck, ".") Then
                    strCheck = Left(strCheck, Len(strCheck) - (InStr(1, strCheck, ".") - 1))
                Else
                    bCK = False
                    GoTo ExitFunction
                End If
            Loop
            If strCheck = "." Or Len(strCheck) = 0 Then bCK = False
    
    ExitFunction:
            ValidEmail = bCK
        Catch ex As ArgumentException
            Return False
        End Try
        Return ValidEmail
    End Function
    
    0 讨论(0)
  • 2020-11-29 06:28

    You could use a Regex to do this.

    There have been written a lot of articles about it; this came up when I searched google for 'regex to validate email address': Find or Validate an Email Address.

    0 讨论(0)
  • 2020-11-29 06:28

    I have tested the approved 'answer' in this case and it does not seem to adhere to the specifications of what actually is a valid email address. After many headaches I found this regex which does a much better job than Microsoft does.

    "(?:(?:\r\n)?[ \t])*(?:(?:(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t]" +
    ")+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|""(?:[^\""\r\\]|\\.|(?:(?:\r\n)?[ \t]))*""(?:(?:" +
    "\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(" +
    "?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|""(?:[^\""\r\\]|\\.|(?:(?:\r\n)?[ " +
    "\t]))*""(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\0" +
    "31]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\" +
    "](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+" +
    "(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:" +
    "(?:\r\n)?[ \t])*))*|(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z" +
    "|(?=[\[""()<>@,;:\\"".\[\]]))|""(?:[^\""\r\\]|\\.|(?:(?:\r\n)?[ \t]))*""(?:(?:\r\n)" +
    "?[ \t])*)*\<(?:(?:\r\n)?[ \t])*(?:@(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\" +
    "r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[" +
    " \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)" +
    "?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t]" +
    ")*))*(?:,@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[" +
    " \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*" +
    ")(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t]" +
    ")+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*)" +
    "*:(?:(?:\r\n)?[ \t])*)?(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+" +
    "|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|""(?:[^\""\r\\]|\\.|(?:(?:\r\n)?[ \t]))*""(?:(?:\r" +
    "\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:" +
    "\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|""(?:[^\""\r\\]|\\.|(?:(?:\r\n)?[ \t" +
    "]))*""(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031" +
    "]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](" +
    "?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?" +
    ":(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?" +
    ":\r\n)?[ \t])*))*\>(?:(?:\r\n)?[ \t])*)|(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?" +
    ":(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|""(?:[^\""\r\\]|\\.|(?:(?:\r\n)?" +
    "[ \t]))*""(?:(?:\r\n)?[ \t])*)*:(?:(?:\r\n)?[ \t])*(?:(?:(?:[^()<>@,;:\\"".\[\] " +
    "\000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|""(?:[^\""\r\\]|" +
    "\\.|(?:(?:\r\n)?[ \t]))*""(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>" +
    "@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|""" +
    "(?:[^\""\r\\]|\\.|(?:(?:\r\n)?[ \t]))*""(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t]" +
    ")*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\" +
    """.\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?" +
    ":[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[" +
    "\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*|(?:[^()<>@,;:\\"".\[\] \000-" +
    "\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|""(?:[^\""\r\\]|\\.|(" +
    "?:(?:\r\n)?[ \t]))*""(?:(?:\r\n)?[ \t])*)*\<(?:(?:\r\n)?[ \t])*(?:@(?:[^()<>@,;" +
    ":\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([" +
    "^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\""" +
    ".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\" +
    "]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*(?:,@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\" +
    "[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\" +
    "r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] " +
    "\000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]" +
    "|\\.)*\](?:(?:\r\n)?[ \t])*))*)*:(?:(?:\r\n)?[ \t])*)?(?:[^()<>@,;:\\"".\[\] \0" +
    "00-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|""(?:[^\""\r\\]|\\" +
    ".|(?:(?:\r\n)?[ \t]))*""(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@," +
    ";:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|""(?" +
    ":[^\""\r\\]|\\.|(?:(?:\r\n)?[ \t]))*""(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*" +
    "(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\""." +
    "\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[" +
    "^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]" +
    "]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*\>(?:(?:\r\n)?[ \t])*)(?:,\s*(" +
    "?:(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\" +
    """.\[\]]))|""(?:[^\""\r\\]|\\.|(?:(?:\r\n)?[ \t]))*""(?:(?:\r\n)?[ \t])*)(?:\.(?:(" +
    "?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[" +
    "\[""()<>@,;:\\"".\[\]]))|""(?:[^\""\r\\]|\\.|(?:(?:\r\n)?[ \t]))*""(?:(?:\r\n)?[ \t" +
    "])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t" +
    "])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?" +
    ":\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|" +
    "\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*|(?:" +
    "[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\" +
    "]]))|""(?:[^\""\r\\]|\\.|(?:(?:\r\n)?[ \t]))*""(?:(?:\r\n)?[ \t])*)*\<(?:(?:\r\n)" +
    "?[ \t])*(?:@(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""" +
    "()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)" +
    "?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>" +
    "@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*(?:,@(?:(?:\r\n)?[" +
    " \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@," +
    ";:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t]" +
    ")*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\" +
    """.\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*)*:(?:(?:\r\n)?[ \t])*)?" +
    "(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\""." +
    "\[\]]))|""(?:[^\""\r\\]|\\.|(?:(?:\r\n)?[ \t]))*""(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:" +
    "\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[" +
    """()<>@,;:\\"".\[\]]))|""(?:[^\""\r\\]|\\.|(?:(?:\r\n)?[ \t]))*""(?:(?:\r\n)?[ \t])" +
    "*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])" +
    "+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\" +
    ".(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z" +
    "|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*\>(?:(" +
    "?:\r\n)?[ \t])*))*)?;\s*)"
    

    I have already formatted it as a vb string using a simple application. Too bad stack overflow is more interested in being a 'coding repository' than having the complete answer for the problem.

    0 讨论(0)
  • 2020-11-29 06:29
     Public Shared Function ValidEmailAddress(ByVal emailAddress As String, ByRef errorMessage As String) As Boolean
            If emailAddress.Length = 0 Then
                errorMessage = "E-mail address is required."
                Return False
            End If
            If emailAddress.IndexOf("@") > -1 Then
                If (emailAddress.IndexOf(".", emailAddress.IndexOf("@")) > emailAddress.IndexOf("@")) AndAlso emailAddress.Split(".").Length > 0 AndAlso emailAddress.Split(".")(1) <> "" Then
                    errorMessage = ""
                    Return True
                End If
            End If
            errorMessage = "E-mail address must be valid e-mail address format."
            Return False
        End Function
    
    0 讨论(0)
提交回复
热议问题