日期:2014-05-18  浏览次数:21008 次

c#检查邮箱格式
怎么样在文本框中检查邮箱的格式用c#,请高手指教!

------解决方案--------------------
C# code

/// <summary>
        /// 检测串值是否为合法的邮件地址格式
        /// </summary>
        /// <param name="strValue">要检测的String值</param>
        /// <returns>成功返回true 失败返回false</returns>
        public static bool CheckIsMailFormat(string strValue)
        {            
            return Utility.CheckIsFormat(@"^([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})(\]?)$", strValue);
        }

        /// <summary>
        /// 检测串值是否为合法的邮件地址格式
        /// </summary>
        /// <param name="strValue">要检测的String值</param>
        /// <returns>成功返回true 失败返回false</returns>
        public static bool CheckIsMailFormatEx(string strValue)
        {
            return Utility.CheckIsFormat(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", strValue);
        }

/// <summary>
        /// 检测串值是否为合法的格式
        /// </summary>
        /// <param name="strRegex">正则表达式</param>
        /// <param name="strValue">要检测的String值</param>
        /// <returns>成功返回true 失败返回false</returns>
        public static bool CheckIsFormat(string strRegex,string strValue)
        {
            if(strValue != null && strValue.Trim() != "")
            {                
                Regex re = new Regex(strRegex);
                if (re.IsMatch(strValue))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            return false;
        }