excel email validation formula

2020-02-02 18:16发布

I have a column where people enter email address manually. I want to validate the email address using this formula:

=AND(FIND(“@”,A2),FIND(“.”,A2),ISERROR(FIND(” “,A2)))

but excel comes up with error that the formula you typed contains an error. For me the formula looks right. Do you guys have any suggestions?

4条回答
Lonely孤独者°
2楼-- · 2020-02-02 18:52

=AND(IFERROR(FIND(".",A2),FALSE),IFERROR(FIND(".",A2,FIND("@",A2)),FALSE))

This will validate the . is after the @ which is not tested on the accepted answer

查看更多
Emotional °昔
3楼-- · 2020-02-02 18:55

I bumped into an issue of firstname.lastname@domain@topdomain for which I made an amendment that checks the correct order of the @ and the . with an implicit Like without VBA.

=AND(NOT(ISERROR(VLOOKUP("*@*.*",A2,1,FALSE))),ISERROR(FIND(" ",A2)))

EDIT
"*?@?*.??*" seems to be even more descriptive as long as top-level domains are at least two characters long (as of this post they are).

查看更多
何必那么认真
4楼-- · 2020-02-02 19:02

I got the same error for your code, and it appears that you have NOT "plain" double quotes, that is different from this symbol: ".

Try my spelling: =AND(FIND("@",A2),FIND(".",A2),ISERROR(FIND(" ",A2))) - hope will help!

EDIT:

In addition, consider to use =AND(NOT(ISERROR(FIND("@",A1))),NOT(ISERROR(FIND(".",A1))),ISERROR(FIND(" ",A1))) - that will prevent errors in case @ or . are missing. Still, this will pass as OK aaa@., but I suppose even such straightforward approach has rights to be used)

查看更多
干净又极端
5楼-- · 2020-02-02 19:10

Another way to validate emails in excel is using VBA code: see code below taken from http://www.vbaexpress.com/kb/getarticle.php?kb_id=281, it works great as is, and you can modify the code based on your needs.

Sub email() 
Dim txtEmail As String 
txtEmail = InputBox("Type the address", "e-mail address") 

Dim Situacao As String 

 ' Check e-mail syntax
If IsEmailValid(txtEmail) Then 
    Situacao = "Valid e-mail syntax!" 
Else 
    Situacao = "Invalid e-mail syntax!" 
End If 
 ' Shows the result
MsgBox Situacao 
End Sub 
Function IsEmailValid(strEmail) 
Dim strArray As Variant 
Dim strItem As Variant 
Dim i As Long, c As String, blnIsItValid As Boolean 
blnIsItValid = True 

i = Len(strEmail) - Len(Application.Substitute(strEmail, "@", "")) 
If i <> 1 Then IsEmailValid = False: Exit Function 
ReDim strArray(1 To 2) 
strArray(1) = Left(strEmail, InStr(1, strEmail, "@", 1) - 1) 
strArray(2) = Application.Substitute(Right(strEmail, Len(strEmail) - Len(strArray(1))), "@", "") 
For Each strItem In strArray 
    If Len(strItem) <= 0 Then 
        blnIsItValid = False 
        IsEmailValid = blnIsItValid 
        Exit Function 
    End If 
    For i = 1 To Len(strItem) 
        c = LCase(Mid(strItem, i, 1)) 
        If InStr("abcdefghijklmnopqrstuvwxyz_-.", c) <= 0 And Not IsNumeric(c) Then 
            blnIsItValid = False 
            IsEmailValid = blnIsItValid 
            Exit Function 
        End If 
    Next i 
    If Left(strItem, 1) = "." Or Right(strItem, 1) = "." Then 
        blnIsItValid = False 
        IsEmailValid = blnIsItValid 
        Exit Function 
    End If 
Next strItem 
If InStr(strArray(2), ".") <= 0 Then 
    blnIsItValid = False 
    IsEmailValid = blnIsItValid 
    Exit Function 
End If 
i = Len(strArray(2)) - InStrRev(strArray(2), ".") 
If i <> 2 And i <> 3 Then 
    blnIsItValid = False 
    IsEmailValid = blnIsItValid 
    Exit Function 
End If 
If InStr(strEmail, "..") > 0 Then 
    blnIsItValid = False 
    IsEmailValid = blnIsItValid 
    Exit Function 
End If 
IsEmailValid = blnIsItValid 
End Function 

For how to instructions check http://www.vbaexpress.com/kb/getarticle.php?kb_id=281#instr

查看更多
登录 后发表回答