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 username@company.com and firstname.lastname@company.com.
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 = "thom.robbins@microsoft.com"
Dim regexp As New Regex("^([\w-]+\.)*?[\w-]+@[\w-]+\.([\w-]+\.)*?[\w]+$")
If regexp.IsMatch(strEmail) Then
MsgBox("Valid Email")
Else
MsgBox("Invalid Email")
End If
End Sub
Comments
- Anonymous
August 23, 2004
The left-hand-side of your regexp needs tightening up -- As it currently stands, it would fail with valid email addresses like rodbegbie+example@gmail.com.
If you look at RFC-2822 you'll see all the (legitimate) characters you can include in the local part of an email address.