• 字符串验证类


    /*****************************************************
     * 文件名:StringValidation.cs
     * 功能描述:扩展方法:字符串验证
     * 创建时间:2014-6-7
     * 作  者: Eric
     * 
     * 修改时间:
     * 修改人:
     * 修改描述
     * 
     ******************************************************/
    public static class StringValidation
        {
            /// <summary>
            /// 验证IP地址
            /// </summary>
            /// <param name="str"></param>
            /// <returns></returns>
            public static bool IsIP(this string str)
            {
                return string.IsNullOrEmpty(str)
                    && Regex.IsMatch(str, @"^(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);
            }
    
            /// <summary>
            /// 验证电话号 如:010-29292929
            /// </summary>
            /// <param name="str"></param>
            /// <returns></returns>
            public static bool IsTelephone(this string str)
            {
                return string.IsNullOrEmpty(str) && Regex.IsMatch(str, @"^d{3,4}-?d{6,8}$", RegexOptions.IgnoreCase);
            }
    
            /// <summary>
            /// 验证手机号 
            /// </summary>
            /// <param name="str"></param>
            /// <returns></returns>
            public static bool IsMobilePhone(this string str)
            {
                return string.IsNullOrEmpty(str) && Regex.IsMatch(str, @"^1[35]d{9}$", RegexOptions.IgnoreCase);
            }
            /// <summary>
            /// 验证邮箱地址
            /// </summary>
            /// <param name="str"></param>
            /// <returns></returns>
            public static bool IsEmail(this string str)
            {
                return string.IsNullOrEmpty(str) && Regex.IsMatch(str, @"^[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="str"></param>
            /// <returns></returns>
            public static bool IsDate(this string str)
            {
                try
                {
                    DateTime time = Convert.ToDateTime(str);
                    return true;
                }
                catch
                {
                    return false;
                }
            }
    
            /// <summary>
            /// 验证是否是中文
            /// </summary>
            /// <param name="str"></param>
            /// <returns></returns>
            public static bool IsChinese(this string str)
            {
                return string.IsNullOrEmpty(str) && Regex.IsMatch(str, @"^[u4e00-u9fa5]+$", RegexOptions.IgnoreCase);
            }
    
            /// <summary>
            /// 验证URL
            /// </summary>
            /// <param name="str"></param>
            /// <returns></returns>
            public static bool IsUrl(this string str)
            {
                return string.IsNullOrEmpty(str) && Regex.IsMatch(str, @"^(((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="str"></param>
            /// <returns></returns>
            public static bool IsZipCode(this string str)
            {
                return string.IsNullOrEmpty(str) && Regex.IsMatch(str, @"^d{6}$", RegexOptions.IgnoreCase);
            }
    
            /// <summary>
            /// 验证QQ
            /// </summary>
            /// <param name="str"></param>
            /// <returns></returns>
            public static bool IsQQ(this string str)
            {
                return string.IsNullOrEmpty(str) && Regex.IsMatch(str, "^[1-9]\d{4,9}$");
            }
    
            /// <summary>
            /// 验证纯字符
            /// </summary>
            /// <param name="str"></param>
            /// <returns></returns>
            public static bool IsLetter(this string str)
            {
                return string.IsNullOrEmpty(str) && Regex.IsMatch(str, "^[a-zA-Z]+$");
            }
    
    
    
            /// <summary>
            /// 验证字符串长度
            /// </summary>
            /// <param name="str"></param>
            /// <param name="begin"></param>
            /// <param name="end"></param>
            /// <returns></returns>
            public static bool IsLength(string str, int begin, int end)
            {
                int length = Regex.Replace(str, @"[^x00-xff]", "OK").Length;
                if ((length <= begin) && (length >= end))
                {
                    return false;
                }
                return true;
            }
    
            //// <summary>
            /// 是不是Int型的
            /// </summary>
            /// <param name="source"></param>
            /// <returns></returns>
            public static bool IsInt(string str)
            {
                Regex regex = new Regex(@"^(-){0,1}d+$");
                if (regex.Match(str).Success)
                {
                    if ((long.Parse(str) > 0x7fffffffL) || (long.Parse(str) < -2147483648L))
                    {
                        return false;
                    }
                    return true;
                }
                return false;
            }
        }
  • 相关阅读:
    phpmyadmi 上传大文件
    wget 命令用法详解
    cuDnn的安装ubuntu16.04环境下(tensorflow正式安装之前的必备安装操作)
    ubuntu16.04 源码安装Python3.7 (可以在此基础上安装Tensorflow) (确保Tensorflow计算框架与系统的彻底隔离)
    大工软件学院 校园网登录脚本
    UML图 之 活动图 (汇总版)
    详解UML图之类图 (转)
    Java中private、protected、public和default的区别 (转)
    AI产业将更凸显个人英雄主义 周志华老师的观点是如此的有深度
    对什么样的人应该敬而远之
  • 原文地址:https://www.cnblogs.com/anbylau2130/p/3775028.html
Copyright © 2020-2023  润新知