Export (0) Print
Expand All

How to: Validate Strings That Represent E-Mail Addresses (Visual Basic)

The following code example sets a Boolean variable that indicates whether a string represents a valid e-mail address.


Function ValidateEmail(ByVal email As String) As Boolean
    Dim emailRegex As New System.Text.RegularExpressions.Regex( 
        "^(?<user>[^@]+)@(?<host>.+)$")
    Dim emailMatch As System.Text.RegularExpressions.Match = 
       emailRegex.Match(email)
    Return emailMatch.Success
End Function


Call this method by passing the string that contains an e-mail address.

This method validates that e-mail addresses have the form "someone@example.com".

Use this code to validate the string before trying to use it as an e-mail address. This could prevent other errors at run time.

Reference

Community Additions

ADD
Show:
© 2014 Microsoft