Home
Manage Your Code
Snippet: Validate EMail (C#)
Title: Validate EMail Language: C#
Description: Validates an email via regular expression. Views: 799
Author: Dave Cooper Date Added: 4/18/2006
Copy Code  
1		/// <summary>
2		///		Validates an input string as a properly formatted email address.
3		/// </summary>
4		/// <param name="eMailAddress">
5		///		System.String, represents
6		///		the email address to be interigates
7		/// </param>
8		public bool isValidEmailAddress( System.String eMailAddress )
9		{
10			/* expression string */
11			System.Text.StringBuilder _stringBuilder = new System.Text.StringBuilder();
12			_stringBuilder.Append( @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" ) ;
13			_stringBuilder.Append( @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" ) ;
14			_stringBuilder.Append( @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$" ) ;
15			
16			/* regular expression */
17			System.Text.RegularExpressions.Regex _regularExpression = new System.Text.RegularExpressions.Regex( _stringBuilder.ToString() ) ;
18
19			/* interigate the string */
20			if ( _regularExpression.IsMatch( (string) eMailAddress ) )
21			{
22				/* is a valid email address */
23				return ( true ) ;
24			}
25			else
26			{
27				/* is not a valid email address */
28				return ( false ) ;
29			}
30		}
Usage
isValidEmailAddress( "name@email.com" ) ;
Notes
returns true if is a valid email address.