excel email validation formula

前端 未结 4 797
心在旅途
心在旅途 2020-12-08 16:04

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(FI         


        
相关标签:
4条回答
  • 2020-12-08 16:05

    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)

    0 讨论(0)
  • 2020-12-08 16:16

    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).

    0 讨论(0)
  • 2020-12-08 16:17

    =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

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题