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( 
    Dim emailMatch As System.Text.RegularExpressions.Match = 
    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 "".

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.


Community Additions