vb.net 文本框电子邮件验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10129374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:03:18  来源:igfitidea点击:

Textbox Email Validation

vb.net

提问by MightyMouse

Hi the problem im having is that im trying to validate a text box to make sure an email address was entered... I copied someone elses code and then changed it to suit my program.. however even when a valid email is entered is still says invalid email entry

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

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

The colours used were green and yellow but I changed the colours of the boxes to identify were the problem was.. the box appears blue so the error ...im assuming is somwere in this snipit of code.

使用的颜色是绿色和黄色,但我改变了盒子的颜色来识别问题是......盒子看起来是蓝色的所以错误......我假设在这段代码中出现了一些错误。

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

Thanks in advance.. :) x

提前致谢.. :) x

回答by Andrew Morton

An easy way to check if an email address is valid is to try creating a MailAddress from it:

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

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

回答by MOB

Check the following code, I have changed a bit of it

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

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

I have stopped the message cause it will never allow to enter valid email unless you copy and paste.

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

Also try this pattern

也试试这个模式

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

回答by user3104295

you are giving the message box in a wrong condition, otherwise your code is correct. Just change coding as below.

您在错误的条件下提供消息框,否则您的代码是正确的。只需更改编码如下。

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

For reference:

以供参考:

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

回答by albin.varghese

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. Pass your email address as string, this is simple and easy. It will return a boolean.
  1. 将您的电子邮件地址作为字符串传递,这很简单。它将返回一个布尔值。