MS访问表的广泛电子邮件验证

时间:2018-04-30 12:24:40

标签: validation access-vba ms-access-2010

嘿伙计们,目前我的表在Access表的“验证规则”部分中进行了以下验证:例如“? @?。?? ”并且不喜欢“ [!az @ =。^ _ $%!#&'`{|} ?〜/] “这使我输入的大部分电子邮件变得干净。

然而,它仍然允许像Bla.Bla@testing.co.u

这样的东西

我找到了java脚本的这个链接,它比我的做得好得多,并且会过滤掉上面提到的那种电子邮件地址。 How to validate an email address in JavaScript?

任何人都知道MS Access的外观如何?功能或验证规则很好,只是想知道它是否可能。

干杯

2 个答案:

答案 0 :(得分:1)

RegExp是验证电子邮件的最佳方式。

这是一个使用您链接的答案中的RegExp的VBA函数

Public Function Email_Validation(ByVal strEmail As String) As Boolean


    Const strRexExp As String = "^(([^<>()\[\]\\.,;:\s@""]+(\.[^<>()\[\]\\.,;:\s@""]+)*)|("".+""))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$"

    Dim objRG As New RegExp
    Dim IsValid As Boolean

    On Error GoTo Err_Handler

    strEmail = Trim(strEmail)

    objRG.IgnoreCase = True
    objRG.Global = True
    objRG.Pattern = strRexExp

    IsValid = objRG.Test(strEmail)


Exit_Function:
    Email_Validation = IsValid
    Exit Function

Err_Handler:
    IsValid = False
    MsgBox "Email_Validation Error: " & Err.Number & vbCrLf & vbCrLf & Err.Description
    Resume Exit_Function
End Function

您必须添加对项目的引用:Microsoft VBScript Regular Expressions X.X

如果要验证,请使用

调用该函数
Email_Validation("Bla.Bla@testing.co.u")

它将返回TRUE或FALSE(在这种情况下为假)

答案 1 :(得分:0)

嘿托马斯非常感谢你的功能真的很有帮助。对于其他人我刚刚发布了我如何实现你的功能。

Public Function Email_Validation(ByVal strEmail As String) As Boolean


    Const strRexExp As String = "^(([^<>()\[\]\\.,;:\s@""]+(\.[^<>()\[\]\\.,;:\s@""]+)*)|("".+""))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$"

    Dim objRG As New RegExp
    Dim IsValid As Boolean

    On Error GoTo Err_Handler

    strEmail = Trim(strEmail)

    objRG.IgnoreCase = True
    objRG.Global = True
    objRG.Pattern = strRexExp

    IsValid = objRG.Test(strEmail)


Exit_Function:
    Email_Validation = IsValid
    Exit Function

Err_Handler:
    IsValid = False
    MsgBox "Email_Validation Error: " & Err.Number & vbCrLf & vbCrLf & Err.Description
    Resume Exit_Function
End Function

Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("Select * FROM Emails WHERE DateAdded =#" & Date & "#;")
Dim Email As String

'Check to see if the table has any rows
If Not (rs.EOF And rs.BOF) Then
    rs.MoveFirst 'Unnecessary in this case, but still a good habit
    Do Until rs.EOF = True
        'Perform an edit
       If Email_Validation(rs!Emails) = True Then
        rs.MoveNext
       Else
        rs.Delete
       End If
    rs.MoveNext
    Loop
Else
MsgBox "There are no records in the recordset."
End If

rs.Close 'Close the recordset
Set rs = Nothing 'Clean up