我目前正在从事Access项目,并且在验证我的项目的电子邮件字段时遇到问题。我希望电子邮件在@符号后有一个强制性字符串,一个强制性@符号以及强制性字母和数字。
当前,我的验证如下:
就像“ * @ *”
这与我想要的完美配合;但是,它仍然接受事先没有字母,数字,句点和破折号的条目。有关如何进行此操作的任何提示和建议,以及可以从中学习验证的任何资源?
答案 0 :(得分:0)
如果验证是在手动输入电子邮件期间发生的,则可以使用掩码。这些是您希望在线阅读的文本字段属性。
如果需要对一组现有数据进行验证,而不是在输入阶段进行验证--或者如果您发现Mask由于某种原因不适合-那么您将需要自定义查询和代码进行检查-和没有一个答案,必须精心设计才能满足您的所有要求。
答案 1 :(得分:0)
尝试:
like "*[@]*[.]*"
上面的意思是“一些字符-任何类型”,则必须有一个@, 然后是“一些字符-任何类型”,然后必须有个。 (点) 然后是一些字符-任何形式。
要在每个部分中强制包含字符,请使用以下方法:
like "?*[@]?*[.]?*"
所以上面是:
must have at least one char (?),
then any number of chars (*),
then MUST have a @ sign ([@]),
then must have at least one char (?),
then any number of chars (*),
then must have a dot ([.]),
then must have one char (?),
then any number of chars (*)
答案 2 :(得分:0)
我们使用此功能-可读:
Public Function IsEmailAddress( _
ByVal strEmailAddresses As String) _
As Boolean
' Checks if strEMailAddr could represent one or more valid e-mail addresses.
' Does not check validity of domain names.
'
' 2003-06-22. Cactus Data ApS, CPH
' 2018-12-01. Expanded to allow for and validate multiple addresses.
' Allowed characters.
Const cstrValidChars As String = "@_-.0123456789abcdefghijklmnopqrstuvwxyz"
Const cstrDot As String = "."
Const cstrAt As String = "@"
' Minimum length of an e-mail address (a@a.ca).
Const cintAddressLenMin As Integer = 6
' Address separator.
Const cstrSeparator As String = ";"
Dim avarAddresses As Variant
Dim Index As Integer
Dim strEmailAddr As String
Dim strValidChars As String
Dim booFailed As Boolean
Dim intPos As Integer
Dim intI As Integer
avarAddresses = Split(strEmailAddresses, cstrSeparator)
For Index = LBound(avarAddresses) To UBound(avarAddresses)
strEmailAddr = avarAddresses(Index)
' Strip a display name.
CleanEmailAddress strEmailAddr
' Convert to lowercase.
strEmailAddr = LCase(strEmailAddr)
' Check that strEMailAddr contains allowed characters only.
For intI = 1 To Len(strEmailAddr)
If InStr(cstrValidChars, Mid(strEmailAddr, intI, 1)) = 0 Then
booFailed = True
End If
Next
If booFailed = False Then
' Check that the first character is not cstrAt.
booFailed = Left(strEmailAddr, 1) = cstrAt
If booFailed = False Then
' Check that the first character is not a cstrDot.
booFailed = Left(strEmailAddr, 1) = cstrDot
If booFailed = False Then
' Check that length of strEMailAddr exceeds
' minimum length of an e-mail address.
intPos = Len(strEmailAddr)
booFailed = (intPos < cintAddressLenMin)
If booFailed = False Then
' Check that none of the last two characters of strEMailAddr is a dot.
booFailed = (InStr(intPos - 1, strEmailAddr, cstrDot) > 0)
If booFailed = False Then
' Check that strEMailAddr does contain a cstrAt.
intPos = InStr(strEmailAddr, cstrAt)
booFailed = (intPos = 0)
If booFailed = False Then
' Check that strEMailAddr does contain one cstrAt only.
booFailed = (InStr(intPos + 1, strEmailAddr, cstrAt) > 0)
If booFailed = False Then
' Check that the character leading cstrAt is not cstrDot.
booFailed = (Mid(strEmailAddr, intPos - 1, 1) = cstrDot)
If booFailed = False Then
' Check that the character following cstrAt is not cstrDot.
booFailed = (Mid(strEmailAddr, intPos + 1, 1) = cstrDot)
If booFailed = False Then
' Check that strEMailAddr contains at least one cstrDot
' following the sign after cstrAt.
booFailed = Not (InStr(intPos, strEmailAddr, cstrDot) > 1)
End If
End If
End If
End If
End If
End If
End If
End If
End If
If booFailed = True Then
Exit For
End If
Next
IsEmailAddress = Not booFailed
End Function
此外,它允许多个地址,例如:
"joe@example.com;ann@domain.org"
此外,如果复制粘贴了地址,则可以使用开头的显示名称来扩展这些地址。这也是允许的,因为在使用此功能进行验证之前会先“清除”地址:
' Strips a full e-mail address with display name like:
'
' "John Doe <john.doe@example.com>"
'
' to the e-mail address only:
'
' "john.doe@example.com"
'
' 2018-12-05. Gustav Brock, Cactus Data ApS, CPH.
'
Public Sub CleanEmailAddress(ByRef EmailAddress As String)
If Trim(EmailAddress) = "" Then
EmailAddress = ""
Else
EmailAddress = Split(StrReverse(Split(StrReverse(EmailAddress), "<")(0)), ">")(0)
End If
End Sub
答案 3 :(得分:0)
另一个使用正则表达式的选项。您可以轻松创建自己的模式,而不用使用if
的色调。
类似这样的东西:
Public Function FN_REGEXP_IS_EMAIL(email As String) As Boolean
If IsBlank(email) Then Exit Function
Const emailPattern As String = "^([\w\-\.]+)@((\[([0-9]{1,3}\.){3}[0-9]{1,3}\])|(([\w\-]+\.)+)([a-zA-Z]{2,4}))$"
On Error Resume Next
With CreateObject("vbscript.RegExp")
.Pattern = emailPattern
FN_REGEXP_IS_EMAIL = .test(email)
End With
End Function