【问题标题】:Email Validation function doesn't work | VB.net电子邮件验证功能不起作用 | VB.net
【发布时间】:2015-03-12 11:48:26
【问题描述】:
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

该函数说电子邮件格式是错误的,但是当按下提交按钮时。即使它不是有效的,它也会发送有效的电子邮件。我做错了什么?

【问题讨论】:

    标签: html asp.net vb.net


    【解决方案1】:

    试试这个。不用emailAddressMatch

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

    代替

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

    【讨论】:

    • 它现在知道电子邮件无效,因为无论是“真”还是“假”,都会出现正确的标签。但是提交按钮仍然可以正常工作并发送虚假电子邮件这可能与提交按钮有关吗??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-17
    • 2021-10-27
    • 2017-05-27
    • 2012-08-19
    • 2019-01-01
    • 2015-01-20
    • 1970-01-01
    相关资源
    最近更新 更多