微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我有一个简单的表单,使用户能够输入促销代码和电子邮件地址,以便注册到电子邮件,如下所示.但目前它没有正确验证电子邮件.

一个包含文件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,".",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/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐