Email address validation for MS Access table

2019-09-28 23:31发布

我的表在,保持大部分的电子邮件地址,我进入清洁Access表的“有效性规则”第以下验证:

“?。?? @”与不似“[!AZ @ = ^ _ $%#&'`{|}〜/ - !?]”和。

然而,它仍然可以让像Bla.Bla@testing.co.u东西

我发现这个链接的JavaScript。 它确实比我更好的工作,将筛选出的那种上面提到的电子邮件地址。 如何验证在JavaScript中的电子邮件地址

它会如何看待的MS Access? 功能或验证规则的罚款只是想知道,如果它是可能的。

Answer 1:

正则表达式是验证电子邮件的最佳途径。

这里是一个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 XX

如果您想验证,使用调用函数

Email_Validation("Bla.Bla@testing.co.u")

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



Answer 2:

嘿,你的很多功能托马斯真的感谢帮助。 对于其他人我刚贴我是如何实现的功能。

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


文章来源: Email address validation for MS Access table