Use regular expression to check email format : Email « Regular Expressions « C# / C Sharp






Use regular expression to check email format

 


//GNU General Public License version 2 (GPLv2)
//http://4mvcblog.codeplex.com/license
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

namespace _4mvcBlog.Core
{
    public static class StringExtensions
    {
        private static readonly Regex EmailExpression = new Regex(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", RegexOptions.Compiled | RegexOptions.Singleline);

        public static bool IsEmail(this string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                return false;
            }
            else
            {
                return EmailExpression.IsMatch(s);
            }
        }
    }
}

   
  








Related examples in the same category

1.Compare two DateTime values
2.Regular expression to match an email address
3.Is email by regular expression