Validating Email

While doing some coding the other day I needed an expression to validate email addresses. The trick was that it had to be flexible enough to handle emails in the format of and


Imports System.Text.RegularExpressions

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim strEmail As String = ""

        Dim regexp As New Regex("^([\w-]+\.)*?[\w-]+@[\w-]+\.([\w-]+\.)*?[\w]+$")


        If regexp.IsMatch(strEmail) Then

            MsgBox("Valid Email")


            MsgBox("Invalid Email")

        End If

End Sub

Comments (1)
  1. Rod says:

    The left-hand-side of your regexp needs tightening up — As it currently stands, it would fail with valid email addresses like

    If you look at RFC-2822 you’ll see all the (legitimate) characters you can include in the local part of an email address.

Comments are closed.

Skip to main content