InStr()asp经典表单字段验证

时间:2012-02-14 14:22:01

标签: asp-classic

我正在尝试使用以下方法检查表单字段中的有效电子邮件地址:

if Request ("email") = "" then
        bError = true
ElseIf Instr(1, email," ") <> 0 Then
        bError = true
ElseIf InStr(1, email, "@", 1) < 2 Then
        bError = true
    else
          */go to success page*/

但如果电子邮件地址中有空格,它仍会通过验证。所以我的问题是,如何使用这种方法检查空格?

3 个答案:

答案 0 :(得分:4)

最好使用正则表达式。

http://classicasp.aspfaq.com/email/how-do-i-validate-an-e-mail-address.html

   Function isEmailValid(email) 
        Set regEx = New RegExp 
        regEx.Pattern = "^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w{2,}$" 
        isEmailValid = regEx.Test(trim(email)) 
    End Function 

答案 1 :(得分:1)

忘掉所有其他的东西,做到这一点很简单......


Dim strEmail
Dim intErrors
intErrors = 0
strEmail = REQUEST("email")
strEmail = Trim(strEmail)
if strEmail = ""           then intErrors = intErrors +1;
if instr(strEmail," ") > 0 then intErrors = intErrors +1;
if instr(strEmail,".") = 0 then intErrors = intErrors +1;
if instr(strEmail,"@") < 2 then intErrors = intErrors +1;

' Put as many test conditions as you want here

if intErrors  = 0 then GotoSuccessPage

答案 2 :(得分:0)

if Request ("email") = "" or Instr(email," ") > 0 or InStr(email, "@") < 2 then
    bError = true
else
    'go to success page
    'BUT ABOUT OTHER ISSUES?

end if 

---------------这是一个非基于REGEXP的电子邮件检查程序,如果它的傻瓜证明不是比提交给你的秘密单位更好......

Function IsEmail(sCheckEmail)

    Dim SEmail, NAtLoc

    IsEmail = True

    SEmail = Trim(sCheckEmail)

    NAtLoc = InStr(SEmail, "@")

    If Not (nAtLoc > 1 And (InStrRev(sEmail, ".") > NAtLoc + 1)) Then

        IsEmail = False

    ElseIf InStr(nAtLoc + 1, SEmail, "@") > NAtLoc Then

        IsEmail = False

    ElseIf Mid(sEmail, NAtLoc + 1, 1) = "." Then

        IsEmail = False

    ElseIf InStr(1, Right(sEmail, 2), ".") > 0 Then

        IsEmail = False

    End If

End Function