0

我写了这个正则表达式函数

    Public Function ValidateEmailAddress(ByVal txtEmailAddress As String) As Boolean

    Dim pattern As String
    pattern = "^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*\\.([a-z]{2,4})$"
    If Regex.IsMatch(txtEmailAddress, pattern) Then
        Return True
    Else
        Return False
    End If

End Function

并在子过程中调用该函数:

  If ValidateEmailAddress(txtEmailAddress.Text) = True Then
    Else
       MessageBox.Show("Email Not Valid")
    End If
    Sub Procedure

当我输入电子邮件时,无论是否正确,我都会收到相同的消息,即电子邮件无效,除了在 If 语句之后单步执行代码时,它会忽略返回 true。请任何人都可以发现这里的问题吗?

4

1 回答 1

2

作为 BigYellowCactus 答案的替代方法:- 您可以使用类似的东西-我认为这比使用正则表达式更可取:

Public Shared Function IsValidEmailAddress(ByVal emailAddress As String) As Boolean
    Try
        Dim address As New MailAddress(emailAddress)
        Return True
    Catch ex As Exception
        Return False
    End Try
End Function

这将返回False@abc.combill@仅检查@将返回True

于 2012-07-26T13:21:16.090 回答