验证电子邮件地址表单提交

我有一个简单的表单,使用户能够输入促销代码和电子邮件地址,以便按照以下方式注册电子邮件。 但目前它不能正确验证电子邮件。

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

我添加了emailValidate来检查电子邮件地址是否有效,如果没有,然后在<%= sys_message%>中告诉用户。

但是,目前它正在阻止真正的电子邮件,所以验证无效。 :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功能来检查电子邮件地址是否有效?

谢谢。


像这样的东西做基本的正则表达式验证。 你可以更喜欢和做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/


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

另一件事 - 你会想看看使用参数化的SQL查询,目前你的命令容易受到SQL注入攻击,因为你只需将代码和电子邮件附加到查询字符串。


这里有几件事关注我。

  • 您没有明确地将表单(“电子邮件”)的值放入会话中,但您稍后在脚本中尝试使用它。 也许你为了简洁而省略了那部分代码,我不知道。

  • 您使用变量“emailValidate”并将其设置为0(即false),但在验证失败时将其设置为1(true)。 这似乎是一个糟糕的变数给我命名。 “失败”应该是假的,“通过”应该是真的。

  • 正如上文所指出的,为了爱你的数据库和所有那些可口的小电子邮件地址,请重构你的代码以避免SQL注入!

  • 你的意见表明你只寻找一个@符号,但是你的代码是说如果你只找到一个@符号,那么emailValidate = 1(失败(在你的代码中)) - 这让我认为你自己的变量命名让你感到困惑!

  • 我不知道你在ASP中编码了多长时间,所以我不喜欢看起来太过于虚伪,但是这种编码方法是不正确的。 这是啰嗦和困惑。 理想的解决方案已经被建议使用正则表达式和一个辅助函数,通过它传递电子邮件地址并返回True或False ...

    链接地址: http://www.djcxy.com/p/92729.html

    上一篇: Validate email address form submit

    下一篇: Best email validation function in general and specific (college domain)?