程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程問題解答 >> 如何判斷電子郵件的地址格式是否正確?

如何判斷電子郵件的地址格式是否正確?

編輯:編程問題解答

第一種辦法:

<%

Function IsValidEmail(Email)

ValidFlag = False

If (Email <> "") And (InStr(1, Email, "@") > 0) And (InStr(1, Email, ".") > 0) Then

atCount = 0

SpecialFlag = False

For atLoop = 1 To Len(Email)

atChr = Mid(Email, atLoop, 1)

If atChr = "@" Then atCount = atCount + 1

If (atChr >= Chr(32)) And (atChr <= Chr(44)) Then SpecialFlag = True

If (atChr = Chr(47)) Or (atChr = Chr(96)) Or (atChr >= Chr(123)) Then SpecialFlag = True

If (atChr >= Chr(58)) And (atChr <= Chr(63)) Then SpecialFlag = True

If (atChr >= Chr(91)) And (atChr <= Chr(94)) Then SpecialFlag = True

Next

If (atCount = 1) And (SpecialFlag = False) Then

BadFlag = False

tAry1 = Split(Email, "@")

UserName = tAry1(0)

DomainName = tAry1(1)

If (UserName = "") Or (DomainName = "") Then BadFlag = True

If Mid(DomainName, 1, 1) = "." then BadFlag = True

If Mid(DomainName, Len(DomainName), 1) = "." then BadFlag = True

ValidFlag = True

' 格式正確返回Ture。

End If

End If

If BadFlag = True Then ValidFlag = False

' 格式不正確返回False。

IsValidEmail = ValidFlag

End Function

%>

 

    第二種辦法:

<%
function IsValidEmail(email)

dim names, name, i, c

IsValidEmail = true
names = Split(email, "@")
if UBound(names) <> 1 then
  IsValidEmail = false
  exit function
end if
for each name in names
  if Len(name) <= 0 then
    IsValidEmail = false
    exit function
  end if
  for i = 1 to Len(name)
    c = Lcase(Mid(name, i, 1))
    if InStr("abcdefghijklmnopqrstuvwxyz_-.", c) <= 0 and not
IsNumeric(c) then
      IsValidEmail = false
      exit function
    end if
  next
  if Left(name, 1) = "." or Right(name, 1) = "." then
      IsValidEmail = false
      exit function
  end if
next
if InStr(names(1), ".") <= 0 then
  IsValidEmail = false
  exit function
end if
i = Len(names(1)) - InStrRev(names(1), ".")
if i <> 2 and i <> 3 then
  IsValidEmail = false
  exit function
end if
if InStr(email, "..") > 0 then
  IsValidEmail = false
end if

end function
%>

    第三種辦法,用下面這個函數進行判斷。它會檢查郵件地址是否含有“@”,以及“.”是否在“@”後面:

function isEmail(pInString)

  lAt = False
  lDot = false

  for x = 2 to len(pInstring)-1
    if mid(pInString,x,1) = "@" then lAt = True
      if mid(pInString,x,1) = "." and lAt = True then lDot = True
  next

  if lAt = True and lDot = True then
    isEmail = True
  else
    isEmail = False
  end if
end function

 

[1]

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved