.net 验证工具类

        /// <summary>
        /// 验证邮箱
        /// </summary>
        /// <param name="source">目标字符串</param>
        /// <returns>验证结果</returns>
         public static bool IsEmail(string source)
        {
            return Regex.IsMatch(source, @"^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$", RegexOptions.IgnoreCase);
        }
        /// <summary>
        /// 验证网址
        /// </summary>
        /// <param name="source">目标字符串</param>
        /// <returns>验证结果</returns>
              public static bool IsUrl(string source)
        {
            return Regex.IsMatch(source, @"^(((file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp)://)|(www\.))+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(/[a-zA-Z0-9\&amp;%_\./-~-]*)?$", RegexOptions.IgnoreCase);
        }
        /// <summary>
        /// 验证是否为日期
        /// </summary>
        /// <param name="source">目标字符串</param>
        /// <returns>验证结果</returns>

public static bool IsDateTime(string source)
        {
            try
            {
                DateTime time = Convert.ToDateTime(source);
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 验证是否为手机号码
        /// </summary>
        /// <param name="source">目标字符串</param>
        /// <returns>验证结果</returns>

public static bool IsMobile(string source)
        {
            return Regex.IsMatch(source, @"^0{0,1}(13[0-9]|14[0-9]|15[0-9]|18[0-9])[0-9]{8}$", RegexOptions.IgnoreCase);
        }
//验证IP
public static bool IsIPV4(string source)
        {
            return Regex.IsMatch(source, @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$", RegexOptions.IgnoreCase);
        }
//验证是不是正常字符 字母,数字,下划线的组合
public static bool IsNormalChar(string source)
        {
            return Regex.IsMatch(source, @"[\w\d_]+", RegexOptions.IgnoreCase);
        }

 

//中文
public static bool IsChinese(string source)
        {
            return Regex.IsMatch(source, @"^[\u4e00-\u9fa5]+$", RegexOptions.IgnoreCase);
        }

 

posted @ 2013-08-16 16:36  无缺~  阅读(184)  评论(0编辑  收藏  举报