文本框电子邮件验证

Mig*_*use 1 vb.net

嗨,我遇到的问题是,我试图验证文本框以确保输入了电子邮件地址...我复制了其他人的代码,然后对其进行更改以适合我的程序。.但是即使输入了有效的电子邮件,仍然说无效的电子邮件输入

Private Sub EmailTextBox_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles EmailTextBox.Validating
    Dim temp As String
    temp = EmailTextBox.Text
    Dim conditon As Boolean
    emailaddresscheck(temp)
    If emailaddresscheck(conditon) = False Then
        MessageBox.Show("Please enter your email address correctly", "Incorrect Email Entry")
        EmailTextBox.Text = ""
        EmailTextBox.BackColor = Color.Blue
    Else
        EmailTextBox.BackColor = Color.Green
    End If

End Sub


Private Function emailaddresscheck(ByVal emailaddress As String) As Boolean
    Dim pattern As String = "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"
    Dim emailAddressMatch As Match = Regex.Match(emailaddress, pattern)
    If emailAddressMatch.Success Then
        emailaddresscheck = True
    Else
        emailaddresscheck = False
    End If
End Function

Private Sub EmailTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EmailTextBox.TextChanged
    EmailTextBox.BackColor = Color.White
    Dim temp As String
    temp = EmailTextBox.Text
    Dim conditon As Boolean
    emailaddresscheck(temp)
    : If emailaddresscheck(conditon) = True Then
        MessageBox.Show("Please enter your email address correctly", "Incorrect Email Entry")
        EmailTextBox.Text = ""
        EmailTextBox.BackColor = Color.Yellow
    Else
        EmailTextBox.BackColor = Color.Green
    End If
End Sub
Run Code Online (Sandbox Code Playgroud)

所使用的颜色是绿色和黄色,但是我更改了框的颜色来确定问题出在..框显示为蓝色,因此错误(即假设是在此代码段中)。

Private Function emailaddresscheck(ByVal emailaddress As String) As Boolean
    Dim pattern As String = "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"
    Dim emailAddressMatch As Match = Regex.Match(emailaddress, pattern)
    If emailAddressMatch.Success Then
        emailaddresscheck = True
    Else
        emailaddresscheck = False
    End If
End Function
Run Code Online (Sandbox Code Playgroud)

在此先感谢.. :) x

And*_*ton 5

一种检查电子邮件地址是否有效的简单方法是尝试从中创建一个MailAddress:

Try
    Dim testAddress = New MailAddress(email)
Catch ex As FormatException
    ' not a valid email address
End Try
Run Code Online (Sandbox Code Playgroud)