0

Does anyone know how to validate email address in excel vba under userform for textbox1?

only got the pattern "^[a-z0-9_.-]+@[a-z0-9.-]{2,}\.[a-z]{2,4}$" but not too sure how to apply it into a function.

so far only have:

        If .textbox1.Value = "" Then
            MsgBox "Please enter email."
            Validation = False
            Exit Function
            
        End If

for validating if theres an empty textbox. if anyone has any solutions it would be highly appreciated

braX
  • 11,506
  • 5
  • 20
  • 33
Megumi
  • 1
  • 1
  • 1
    Does this answer your question? [How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops](https://stackoverflow.com/questions/22542834/how-to-use-regular-expressions-regex-in-microsoft-excel-both-in-cell-and-loops) – Leandro Bardelli Nov 28 '21 at 16:08
  • Here are some suggestions: [Doing Email Validations](https://stackoverflow.com/questions/54315493/doing-email-validations-for-microsoft-access/54322970#54322970). – Gustav Nov 28 '21 at 16:26

1 Answers1

0
Function IsEmail_(sEmail$) As Boolean
Dim arr, iPos As Long, iLen As Long

arr = Split(sEmail$, "@")

If UBound(arr) - LBound(arr) <> 1 Then Exit Function
If Len(arr(0)) < 1 Then Exit Function

iLen = Len(arr(1))
If iLen < 3 Then Exit Function

iPos = InStr(1, arr(1), ".", vbBinaryCompare)
If iPos <= 1 Then Exit Function
If iPos = iLen Then Exit Function

IsEmail_ = True
End Function
Lee
  • 1
  • Hey, @Lee. Could you please indent (Beautify) your code, and, then, enriches the code with what are the rules it follows? Like: (a) there must be a `@`; (b) after the `@` must be a string with at least three chars; (c) (I gave up to understand :-) ) – Marcus Vinicius Pompeu Nov 28 '21 at 20:24
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 28 '21 at 20:25