码迷,mamicode.com
首页 > Windows程序 > 详细

C# 校验 正则

时间:2017-07-06 14:15:27      阅读:291      评论:0      收藏:0      [点我收藏+]

标签:邮箱   存在   res   ram   地址   als   match   ret   options   

邮箱验证

/// <summary>
/// 是否邮箱
/// </summary>
/// <param name="value">邮箱地址</param>
/// <param name="isRestrict">是否按严格模式验证</param>
/// <returns></returns>
public static bool IsEmail(string value, bool isRestrict=false)
{
    if (value.IsEmpty())
    {
        return false;
    }
    string pattern = isRestrict
        ? @"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&‘\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$"
        : @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$";
       
    return value.IsMatch(pattern, RegexOptions.IgnoreCase);
}

/// <summary>
/// 是否存在邮箱
/// </summary>
/// <param name="value">值</param>
/// <param name="isRestrict">是否按严格模式验证</param>
/// <returns></returns>
public static bool HasEmail(string value, bool isRestrict = false)
{
    if (value.IsEmpty())
    {
        return false;
    }
    string pattern = isRestrict
        ? @"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&‘\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$"
        : @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$";
    return value.IsMatch(pattern, RegexOptions.IgnoreCase);
}

C# 校验 正则

标签:邮箱   存在   res   ram   地址   als   match   ret   options   

原文地址:http://www.cnblogs.com/D-LuFei/p/7125833.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!