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.

Example

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

Compiling the Code

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

Robust Programming

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.

See Also

Reference

Regex