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

前端 未结 11 629
孤城傲影
孤城傲影 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
    

提交回复
热议问题