2012-02-14 68 views
1

我試圖使用表單字段以檢查有效的電子郵件地址:InStr函數()ASP經典表單字段驗證

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

您首先需要爲請求值分配一個名爲「email」的變量:'email = Request(「email」)'。 – 2012-02-15 07:28:30

回答

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 
+0

是的,我知道reg ex會更好,但我不能在這種情況下使用它。 – 2012-02-14 15:03:04

+1

爲什麼不呢?你只是把地址傳給一個函數。 – 2012-02-14 15:06:53

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基於電子郵件檢查,不能肯定是否ITS FOOL PROOF但比提交片斷應該讓你去...

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 
1

忘掉所有的東西ELSEIF做簡單...


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