5

I want to check if email address is valid or not.

How can an email address be validated in VB using Regular Expressions?

([\w-+]+(?:\.[\w-+]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7})
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
Sanya Zahid
  • 466
  • 6
  • 18
  • Possible duplicate of [Validating e-mail with regular expression VB.Net](http://stackoverflow.com/questions/369543/validating-e-mail-with-regular-expression-vb-net) – Jodrell Mar 22 '16 at 08:45
  • Shouldn't you simplify your question to, "how do I match a regular expression in VB.Net?" – Jodrell Mar 22 '16 at 08:52

3 Answers3

3

MSDN Article: How to: Verify That Email are in Valid E-Mail Format

This example method calls the Regex.IsMatch(String, String) method to verify that the string conforms to a regular expression pattern.

Function IsValidEmailFormat(ByVal s As String) As Boolean
    Return Regex.IsMatch(s, "^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$")
End Function
1
    Function validateEmail(emailAddress) As Boolean

    Dim email As New Regex("([\w-+]+(?:\.[\w-+]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7})")
    If email.IsMatch(emailAddress) Then
        Return True
    Else
        Return False
    End If
Sanya Zahid
  • 466
  • 6
  • 18
0

This is my approach without messing with RegEx, taken from my ElektroKit API:

enter image description here

''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Validates a mail address.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <example> This is a code example.
''' <code>
''' Dim isValid As Boolean = ValidateMail("Address@Gmail.com")
''' Dim isValid As Boolean = ValidateMail("mailto:Address@Gmail.com")
''' </code>
''' </example>
''' ----------------------------------------------------------------------------------------------------
''' <param name="address">
''' The mail address.
''' </param>
''' ----------------------------------------------------------------------------------------------------
''' <returns>
''' <see langword="True"/> if the mail address is valid, <see langword="False"/> otherwise.
''' </returns>
''' ----------------------------------------------------------------------------------------------------
<DebuggerStepThrough>
Public Shared Function ValidateMail(ByVal address As String) As Boolean

    If Not address.StartsWith("mailto:", StringComparison.OrdinalIgnoreCase) Then
        address = address.Insert(0, "mailto:")
    End If

    If Not Uri.IsWellFormedUriString(address, UriKind.Absolute) Then
        Return False
    End If

    Dim result As Uri = Nothing
    If Uri.TryCreate(address, UriKind.Absolute, result) Then
        Return (result.Scheme = Uri.UriSchemeMailto)

    Else
        Return False

    End If

End Function

Note: I didn't performed a deep test with huge lists of emails.


If you preffer a regular expression, then this is my own following Wikipedia especifications, it gives me better results than other "universal" expressions, however, this expression is not perfectlly constructed, it doesn't avoid for example the double-dot conflict specified in Wikipedia, but we assume that will not encounter strange naming exceptions like that.

This expression never gave me a false positive (by the moment), but as any other Regex expr, use it at your own risk:

enter image description here

''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' A pattern that matches an e-mail address.
''' <para></para>
''' 
''' For Example:
''' <para></para>
''' <c>local@domain.com</c>
''' 
''' </summary>
''' ----------------------------------------------------------------------------------------------------
Public Const EMail As String =
    "^(?<address>(?<local>[\d\w!#\$%\&'\*\+-/=\?\^_`\\\|~""\(\),:;\<\>@\[\]\{\}\.]+)@(?<domain>[\d\w-\.]+)\..+)$"
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417