Net-informations.com
SiteMap  | About    

C# email validation

An email address is a string of a subset of ASCII characters separated into two parts by an @ symbol . The part before the @ sign is the local part of the address, and the part after the @ sign is a domain name to which the email message will be sent .

  feedback@net-informations.com

Trying to match these restrictions is a complex task, often resulting in long regular expressions. The .NET Framework regular expression classes are part of the base class library and can be used with any language or tool that targets the common language runtime, including ASP.NET and Visual Studio .NET.

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

Regex.IsMatch Method (String, String) - indicates whether the specified regular expression finds a match in the specified input string. The pattern parameter consists of various regular expression language elements that symbolically describe the string to match pattern . The following C# source code shows how to validate an email address with the help of regular expressions.




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 ");
			}
		}
	}
}






net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.