【问题标题】:Validate email address form submit验证电子邮件地址表单提交
【发布时间】:2023-04-10 03:00:01
【问题描述】:

我有一个简单的表单,用户可以输入促销代码和电子邮件地址以注册电子邮件,如下所示。但目前它无法正确验证电子邮件。

有一个包含文件 doreferral.asp;检查他们输入的代码是否存在于促销代码表中,并检查电子邮件地址是否已经存在。

我已经添加了 emailValidate 来检查邮箱地址是否有效,如果无效,然后在 中告诉用户。

但是,它目前正在阻止真正的电子邮件,因此验证不起作用。 :S

我的 doreferral.asp 是这样的;

<%
    Code            = replace(request.Form("Code"),"'","")
    Email       = replace(request.Form("Email"),"'","")

    sys_message = ""
    submission = ""

    ''//Check the submitted code against existing ones in the database
    set conn = server.CreateObject("ADODB.connection")
    conn.open(application("DATABASE"))
    qs = "SELECT COUNT(AgentReferralCode) AS 'CountCodes' FROM Customers WHERE AgentReferralCode = '" & Code & "'"
    set rs = conn.Execute(qs)

    CountCode = rs("CountCodes")

    set rs = nothing
    conn.close
    set conn = nothing

    If(CountCode < 1) Then
        sys_message = sys_message & "<p class='err'>The agent code does not exist.</p>"
    End If

''//Check to see if the email address is valid
Dim emailValidate
emailValidate = 0 'Initializing goby to 0

''//if the len is less than 5 then it can't be an email
''//(i.e.: a@a.c) 
If Len(session("Email")) <= 5 Then
   emailValidate = 1
End If

If InStr(1, session("Email"), "@", 1) < 2 Then
    'If we find one and only one @, then the
    'email address is good to go.
    emailValidate = 1
Else
    If InStr(1,session("Email"), ".", 1) < 4 Then
        'Must have a '.' too
         emailValidate = 1
    End If
End If

If emailValidate <> 0 then 
    sys_message = sys_message & "<p class='err'>The email address is not valid.</p>"
End If

    ''//Check the submitted email against existing ones in the database
    set conn = server.CreateObject("ADODB.connection")
    conn.open(application("DATABASE"))
    qs = "SELECT COUNT(ReferredEmail) AS 'Count' FROM TenantReferral WHERE ReferredEmail = '" & Email & "'"
    set rs = conn.Execute(qs)

    countEmail = rs("Count")

    set rs = nothing
    conn.close
    set conn = nothing

    If(countEmail >= 1) Then
        sys_message = sys_message & "<p class='err'>This email address has already been referred.</p>"
    End If  

    ''//Only Process the SQL if there is no sys_message
    If(sys_message = "") Then

        SQLfields = SQLfields & "ReferredCode, "
        SQLvalues = SQLvalues & "'"& Trim(Code) &"', "
        SQLfields = SQLfields & "ReferredEmail"
        SQLvalues = SQLvalues & "'"& Trim(Email) &"'"

        SQL = SQL & "INSERT into TenantReferral ("& SQLfields &") VALUES ("& SQLvalues &")"
        'response.Write(SQL)

        set conn = server.CreateObject("ADODB.connection")
        conn.open application("DATABASE")
        SET rs = conn.execute(SQL)

        [Send email code]


        sys_message = sys_message & "<p class='ok'>Thank you for your referral.</p>" 
        submission = "ok"
        'response.Redirect("referral.asp")
    End If
%>

我想知道是否有人可以帮助调试 emailValidate 功能以检查电子邮件地址是否有效?

谢谢。

【问题讨论】:

标签: email asp-classic validation


【解决方案1】:

这样的东西可以进行基本的正则表达式验证。您可以更高级地进行 dns 查找,但对于大多数目的而言,这已经足够了:

  Function validate(eaddr)
  dim isValidE
  dim regEx

  isValidE = True
  set regEx = New RegExp

  regEx.IgnoreCase = False

  regEx.Pattern = "^[-+.\w]{1,64}@[-.\w]{1,64}\.[-.\w]{2,6}$"
  isValidE = regEx.Test(eaddr)

  validate= isValidE
  End Function

从这里借来的正则表达式:http://tiffanybbrown.com/2006/12/12/a-better-regex-pattern-for-matching-e-mail-addresses/

【讨论】:

    【解决方案2】:

    这是一个包含示例电子邮件正则表达式验证器的页面:http://www.codetoad.com/asp_email_reg_exp.asp。您应该能够使用类似的东西来满足您的需求。

    还有一件事——你会想看看使用参数化的 SQL 查询,目前你的命令容易受到 SQL 注入的影响,因为你只是将代码和电子邮件附加到查询字符串。

    【讨论】:

      【解决方案3】:

      这里有几件事让我担心。

      1. 您没有明确地将 form("email") 值放入会话中,但您稍后会尝试在脚本中使用它。可能你为了简洁省略了那部分代码,我不知道。

      2. 您使用变量“emailValidate”并将其设置为 0(即假),但在验证失败时将其设置为 1(真)。这对我来说似乎是错误的变量命名。 “失败”应为 False,“通过”应为 True。

      3. 如上所述,为了您的数据库和所有这些美味的实时电子邮件地址,请重构您的代码以避免 SQL 注入!

      4. 您的 cmets 声明您正在寻找一个 @ 符号,但您的代码说如果您只找到一个 @ 符号然后 emailValidate = 1(失败(在您的代码中))——这导致我认为你自己的变量命名让你感到困惑!

      我不知道您在 ASP 中编码有多久了,所以我不愿意给人一种太道貌岸然的印象,但是这种编码方法是不正确的。这是长篇大论和困惑。理想的解决方案已经建议使用正则表达式和一个小助手函数,您可以将电子邮件地址传递到该函数中,然后只返回 True 或 False...

      【讨论】:

        猜你喜欢
        • 2013-11-25
        • 2013-05-04
        • 1970-01-01
        • 2014-06-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-13
        • 2011-09-02
        相关资源
        最近更新 更多