Email Address Validation

An email address is a string comprising a subset of ASCII characters that is divided into two parts by the "@" symbol. The section preceding the "@" sign is known as the local part of the address, while the portion following the "@" sign represents the domain name to which the email message will be directed. In the provided example, "feedback" is the local part, and "net-informations.com" is the domain name.

email-validation

To ensure the validity of email addresses, we can employ a regular expression pattern for validation purposes. Regular expressions consist of patterns that describe specific text sequences. In the .NET Framework, regular expression classes are part of the base class library and can be utilized in various programming languages and tools targeting the common language runtime, including ASP.NET and Visual Studio .NET.

Regular expressions

One useful method for working with regular expressions is the Regex.IsMatch method, which determines whether a given input string matches a specified regular expression pattern. The pattern parameter incorporates different elements of the regular expression language, symbolically defining the pattern to be matched within the string.

To illustrate the implementation of email address validation using regular expressions in an ASP.NET program, the following code snippet can be employed:

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

The pattern parameter consists of various regular expression language elements that symbolically describe the string to match pattern . The following ASP.NET program shows how to validate an email address with the help of regular expressions.

Default.aspx
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="Label1" runat="server" Text="Enter e-mail address"></asp:Label> <br /> <asp:TextBox ID="TextBox1" runat="server" Width="284px"></asp:TextBox> <br /> <br /> <asp:Button ID="Button1" runat="server" onclick="Button1_Click1" Text="Validate" Width="80px" />   <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label> </div> </form> </body> </html>
Full Source | C#
using System; using System.Text.RegularExpressions; public partial class _Default : System.Web.UI.Page { protected void Button1_Click1(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(TextBox1.Text , pattern)) { Label2.Text = "Valid Email address "; } else { Label2.Text = "Not a valid Email address "; } } }
Full Source | VB.NET
Imports System.Net.Mail Imports System.Text.RegularExpressions Partial Class _Default Inherits System.Web.UI.Page Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim pattern As String 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(TextBox1.Text, pattern) Then Label2.Text = "Valid Email address " Else Label2.Text = "Not a valid Email address " End If End Sub End Class

Conclusion

Using regular expressions, developers can effectively validate the format and structure of email addresses, ensuring that they adhere to the expected pattern and minimizing the risk of accepting incorrect or malformed email addresses within their applications.