电子邮件验证功能不起作用VB.net

时间:2015-03-12 11:48:26

标签: html asp.net vb.net

Protected Sub btn_Submit_Click(sender As Object, e As EventArgs) Handles btn_Submit.Click
    Dim _lblresponse As Label = Nothing
    _lblresponse = New Label

    Dim MyEmail As String = txb_Email.Text
    If EmailCheck(MyEmail) = True Then
        Dim _lblresponse_Valid As Label = Nothing
        _lblresponse = New Label
    Else
        Dim _lblresponse_Invalid As Label = Nothing
        _lblresponse = New Label
        _lblresponse.Text = "This is not a Valid Email"
        Me.plh_Response_Invalid.Controls.Add(_lblresponse)
    End If

    _lblresponse.Text = "*Your Request Has Been Sent"
    Me.plh_Response.Controls.Add(_lblresponse)




Function EmailCheck(ByVal emailAddress As String) As Boolean


    Dim pattern As String = "^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"
    Dim emailAddressMatch As Match = Regex.Match(emailAddress, pattern)


    If emailAddressMatch.Success Then
        Return True
    Else
        Return False
    End If

End Function

该函数表示电子邮件格式为false,但按下提交按钮时。它会将电子邮件发送为有效,即使它不是。我做错了什么?

1 个答案:

答案 0 :(得分:0)

试试这个。无需使用emailAddressMatch

If Regex.IsMatch(emailAddress, pattern) then
  return true
Else
  return false
End If

而不是

Dim emailAddressMatch As Match = Regex.Match(emailAddress, pattern)