关闭

NET C/S 如何使用正册表达式

448人阅读 评论(0) 收藏 举报
 class VilControl
    {
        /// <summary>
        /// 判断是否被未空
        /// </summary>
        /// <returns>为空返回false</returns>
        public static bool IsNull(string syllable)
        {
            if (syllable.Trim() == "") //判断是否为空输入
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// 验证字符串是否合法
        /// </summary>
        /// <param name="syllable">需验证字符串</param>
        /// <param name="regex">正则表达式</param>
        /// <returns></returns>
        private static bool RegexOperation(string syllable, string regex)
        {
            //正则表达式的枚举类型
            System.Text.RegularExpressions.RegexOptions options = (
                (System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace |
                System.Text.RegularExpressions.RegexOptions.Multiline) |
                System.Text.RegularExpressions.RegexOptions.IgnoreCase);

            //加载正则表达式到枚举类型上
            System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(regex, options);

            //返回验证结果
            return reg.IsMatch(syllable);
        }

        /// <summary>
        /// 判断密码是否超出长度(6~16)
        /// </summary>
        /// <returns>超长返回false</returns>
        public static bool IsLong(string syllable)
        {
            if (!IsNull(syllable)) //判断是否为空输入
            {
                return false;
            }

            bool problem = true;

            if (syllable.Length > 16 || syllable.Length < 6) //判断是否符合密码位数
            {
                return problem = false;
            }
            return problem;
        }

        /// <summary>
        /// 年龄(长度/整数)(1~110)
        /// </summary>
        /// <returns>不正确返回false</returns>
        public static bool IsAge(string syllable)
        {
            if (!IsNull(syllable)) //判断是否为空输入
            {
                return false;
            }

            if ((Convert.ToInt32(syllable) > 110 || Convert.ToInt32(syllable) < 1))//判断长度
            {
                return false;
            }

            string regex = "^[0-9]{1,}$"; //判断为数字

            return RegexOperation(syllable, regex);
        }

        /// <summary>
        /// 是否符合电话号码的要求(3-8,4-7,11)
        /// </summary>
        /// <returns>不符合返回false</returns>
        public static bool IsPhone(string syllable)
        {
            //判断是否为空输入
            if (!IsNull(syllable))
            {
                return false;
            }

            //正则表达式
            string regex = @"/d{3}-/d{8}|/d{4}-/d{7}|/d{11}";

            //返回验证结果
            return RegexOperation(syllable, regex);
        }
   

        /// <summary>
        /// 是否符合日期的要求(2002-02-02)
        /// </summary>
        /// <returns>不符合返回false</returns>
        public static bool IsDay(string syllable)
        {
            //判断是否为空输入
            if (!IsNull(syllable))
            {
                return false;
            }

            //正则表达式
            string regex = @"^((((1[6-9]|[2-9]/d)/d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]/d|3[01]))|(((1[6-9]|[2-9]/d)/d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]/d|30))|(((1[6-9]|[2-9]/d)/d{2})-0?2-(0?[1-9]|1/d|2[0-8]))|(((1[6-9]|[2-9]/d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$";

            //返回验证结果
            return RegexOperation(syllable, regex);
        }

        /// <summary>
        /// 是否符合电子邮件的要求
        /// </summary>
        /// <returns>不符合返回false</returns>
        public static bool IsE_Mail(string syllable)
        {
            //判断是否为空输入
            if (!IsNull(syllable))
            {
                return false;
            }

            //正则表达式
            string regex = @"/w+([-+.]/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*";

            //返回验证结果
            return RegexOperation(syllable, regex);
        }

        /// <summary>
        /// 由数字和26个英文字母或者下划线组成的字符串
        /// </summary>
        /// <param name="strVerify"></param>
        /// <returns></returns>
        public static bool StrVerify(string syllable)
        {
            //判断是否为空输入
            if (!IsNull(syllable))
            {
                return false;
            }

            string regex = @"^/w+$";
            return RegexOperation(syllable, regex);
        }

        /// <summary>
        /// 只能输入有1~3位小数的正实数
        /// </summary>
        /// <param name="strVerify"></param>
        /// <returns></returns>
        public static bool NumDec(string syllable)
        {
            //判断是否为空输入
            if (!IsNull(syllable))
            {
                return false;
            }

            //正则表达式
            string regex = @"^[0-9]+(.[0-9]{1,3})?$";

            //返回验证结果
            return RegexOperation(syllable, regex);
        }

        /// <summary>
        /// 以字母开头,长度在6到18,只能包含字符、数字和下划线
        /// </summary>
        /// <param name="strVerify"></param>
        /// <returns></returns>
        public static bool StrVerifyStart(string syllable)
        {
            //判断是否为空输入
            if (!IsNull(syllable))
            {
                return false;
            }

            //正则表达式
            string regex = @"^[a-zA-Z]/w{5,17}$";

            //返回验证结果
            return RegexOperation(syllable, regex);
        }
    }

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:385746次
    • 积分:7177
    • 等级:
    • 排名:第3125名
    • 原创:325篇
    • 转载:71篇
    • 译文:1篇
    • 评论:34条
    最新评论