C# email validation

An email address is essentially a string composed of a specific subset of ASCII characters. It is divided into two parts by the @ symbol. The portion preceding the @ sign is referred to as the local part of the address, while the segment following the @ sign denotes the domain name to which the email message will be sent. Enforcing these restrictions and validating email addresses can be a complex task, often necessitating the use of lengthy regular expressions.

Regular expression classes

Fortunately, the .NET Framework provides a set of regular expression classes as part of its base class library. These classes can be utilized with any language or tool that targets the common language runtime (CLR), such as ASP.NET and Visual Studio .NET.


One particularly useful method in the .NET Framework for working with regular expressions is the Regex.IsMatch method. This method enables developers to determine whether a given input string matches a specified regular expression pattern. The pattern parameter in this method comprises various elements of the regular expression language, which symbolically describe the pattern that needs to be matched within the input string.

Regex.IsMatch("feedback@net-informations.com", pattern)

Regex.IsMatch method

Utilizing the Regex.IsMatch method and using the power of regular expressions, developers can effectively validate and manipulate email addresses in their applications. This capability allows for robust email handling, ensuring that email-related operations are performed accurately and securely.

Full Source C#
using System; using System.Windows.Forms; using System.Text.RegularExpressions; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string pattern = null; pattern = "^([0-9a-zA-Z]([-\\.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$"; if (Regex.IsMatch("feedback@net-informations.com", pattern)) { MessageBox.Show ("Valid Email address "); } else { MessageBox.Show("Not a valid Email address "); } } } }