问题
I'm trying to validate emails but my code is not allowing a few scenarios..
Code
pvm = "bw.stack@domain.com" does not work
pvm = "user@gmail.com" - works
If Len(pvm) < 5 OR NOT Instr(1, pvm, " ") = 0 OR InStr(1, pvm, "@", 1) < 2 OR InStrRev(pvm, ".") < InStr(1, pvm, "@", 1) Then
blnEmailOKREG = False
Else
blnEmailOKREG = True
End If
回答1:
Here is a fairly good valiation for email addresses. Please note though that its hard to get a completely 'all-knowing' validation.
' Function IsValidEmail
' Method checks if a string value is a valid email adress
' @Param val: <string> String containing email adress.
' @RETURN: True / False
Public function IsValidEmail(ByRef val)
IsValidEmail = RegExTest(val,"^[_a-zA-Z0-9-""'\/]+(\.[_a-zA-Z0-9-""'\/]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|coop|info|museum|name))$")
end function
_
Public function RegexTest(str, pattern)
dim m_objRegex
set m_objRegex = New RegExp
m_objRegex.Pattern = pattern
m_objRegex.Global = True
RegExTest = m_objRegex.Test(str)
set m_objRegex = nothing
End function
来源:https://stackoverflow.com/questions/6936251/email-validation-serverside