In this article I have explained with an example, how to perform Email address validation using Regular Expression in Windows Forms (WinForms) Application using C# and VB.Net.
 
 
Form Design
The following Form consists of one TextBox, two Labels and a Button control.
Email Address validation using RegularExpressionValidator in C# and VB.Net
 
 
Namespaces
You will need to import the following namespace.
C#
using System.Text.RegularExpressions;
 
VB.Net
Imports System.Text.RegularExpressions
 
 
Regular Expression (Regex) to validate Email address
Regular Expression (Regex)
^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$
 
Explanation
Above Regular Expression validates Email address.
 
Example
When the Validate Button is clicked, the value of the TextBox is validated against the Regular Expression (Regex) and if invalid, the error Label is displayed.
C#
 
private void OnValidate(object sender, EventArgs e)
{
    lblError.Hide();
    Regex regex = new Regex(@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
    if (!regex.IsMatch(txtEmail.Text.Trim()))
    {
        lblError.Show();
    }
}
 
VB.Net
Private Sub OnValidate(sender As Object, e As EventArgs) Handles btnValidate.Click
    lblError.Hide()
    Dim regex As Regex = New Regex("^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$")
    If Not regex.IsMatch(txtEmail.Text.Trim()) Then
        lblError.Show()
    End If
End Sub
 
 
Demo
Email Address validation using RegularExpressionValidator in C# and VB.Net
 
 
Downloads