文本框电子邮件验证

时间:2012-04-12 18:16:00

标签: 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

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

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

先谢谢.. :) x

4 个答案:

答案 0 :(得分:5)

检查电子邮件地址是否有效的简便方法是尝试从中创建邮件地址:

Try
    Dim testAddress = New MailAddress(email)
Catch ex As FormatException
    ' not a valid email address
End Try

答案 1 :(得分:2)

检查以下代码,我已经改变了一点

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

    End Sub

我已停止该消息,因为除非您复制并粘贴,否则永远不会允许输入有效的电子邮件。

也尝试这种模式

Dim pattern As String = "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"

答案 2 :(得分:0)

您在错误的情况下给出了消息框,否则您的代码是正确的。 只需更改编码如下。

If emailaddresscheck(conditon) = True Then
           msgbox ("Correct email id")
Else
   MessageBox.Show("Please enter your email address correctly", "Incorrect Email Entry")
    EmailTextBox.Text = ""

End If

供参考:

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 pattern As String = "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
        Dim emailAddressMatch As Match = Regex.Match(emailAddress, pattern)
        If emailAddressMatch.Success Then
            EmailAddressCheck = True
        Else
            EmailAddressCheck = False
        End If

End Function

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        'EmailAddressCheck(TextBox6.Text)
        If EmailAddressCheck(TextBox6.Text) = True Then
            MessageBox.Show("Please enter your email address correctly", "Incorrect Email Entry")
            TextBox6.Text = ""
        Else
            MsgBox("correct")
        End If
End Sub

答案 3 :(得分:0)

P

rivate Function validateEmail(ByVal Email As String)
        Try
            Dim myEmails As String() = Email.Split(",")
            Dim isV As Boolean = True
            For i As Integer = 0 To myEmails.Length - 1
                ' Validate One by Ibe
                isV = Global.ValidateEmail.IsValidEmail(myEmails(i).ToString)
                If (isV = False) Then
                    Return False
                End If
            Next
            Return isV
        Catch ex As Exception
        End Try
    End Function
  1. 将您的电子邮件地址作为字符串传递,这很简单。它将返回一个布尔值。