• c#結合正則表達式驗證輸入的字符串


    using System.Text.RegularExpressions;

    #region 驗證輸入的字符串
        /// <summary>
        /// 判斷輸入的字符類型 
        /// </summary>
        /// <param name="_value">輸入的字串</param>
        /// <param name="_kind">要驗證的類型</param>
        /// 1: 由26個英文字母組成的字串
        /// 2: 正整數
        /// 3: 非負整數(正整數 + 0)
        /// 4: 非正整數(負整數 + 0)
        /// 5: 負整數
        /// 6: 整數
        /// 7: 非負浮點數(正浮點數 + 0)
        /// 8: 正浮點數
        /// 9: 非正浮點數(負浮點數 + 0)
        /// 10: 負浮點數
        /// 11: 浮點數
        /// 12: 由26個英文字母的大寫組成的字串
        /// 13: 由26個英文字母的小寫組成的字串
        /// 14: 由數位和26個英文字母組成的字串
        /// 15: 由數位、26個英文字母或者下劃線組成的字串
        /// 16: Email
        /// 17: URL
        /// 18: 只能輸入入中文
        /// 19:
        /// </param>
        /// <returns>true是驗証通過,false是驗証錯誤</returns>
        /// <returns></returns>

        public  bool ValidateUserInput(String _value, int _kind)
        {
            string RegularExpressions = null;

            switch (_kind)
            {
                case 1:
                    //由26個英文字母組成的字串
                    RegularExpressions = "^[A-Za-z]+$";
                    break;
                case 2:
                    //正整數
                    RegularExpressions = "^[0-9]*[1-9][0-9]*$";
                    break;
                case 3:
                    //非負整數(正整數 + 0)
                    RegularExpressions = "^\\d+$";
                    break;
                case 4:
                    //非正整數(負整數 + 0)
                    RegularExpressions = "^((-\\d+)|(0+))$";
                    break;
                case 5:
                    //負整數
                    RegularExpressions = "^-[0-9]*[1-9][0-9]*$";
                    break;
                case 6:
                    //整數
                    RegularExpressions = "^-?\\d+$";
                    break;
                case 7:
                    //非負浮點數(正浮點數 + 0)
                    RegularExpressions = "^\\d+(
    \\.\\d+)?$";
                    break;
                case 8:
                    //正浮點數
                    RegularExpressions = "^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$";
                    break;
                case 9:
                    //非正浮點數(負浮點數 + 0)
                    RegularExpressions = "^((-\\d+(
    \\.\\d+)?)|(0+(\\.0+)?))$";
                    break;
                case 10:
                    //負浮點數
                    RegularExpressions = "^(-(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*)))$";
                    break;
                case 11:
                    //浮點數
                    RegularExpressions = "^(-?\\d+)(
    \\.\\d+)?$";
                    break;
                case 12:
                    //由26個英文字母的大寫組成的字串
                    RegularExpressions = "^[A-Z]+$";
                    break;
                case 13:
                    //由26個英文字母的小寫組成的字串
                    RegularExpressions = "^[a-z]+$";
                    break;
                case 14:
                    //由數位和26個英文字母組成的字串
                    RegularExpressions = "^[A-Za-z0-9]+$";
                    break;
                case 15:
                    //由數位、26個英文字母或者下劃線組成的字串
                    RegularExpressions = "^[0-9a-zA-Z_]+$";
                    break;
                case 16:
                    //email地址
                    RegularExpressions = "
    \\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
                    break;
                case 17:
                    //url
                    RegularExpressions = "^[a-zA-z]+://(
    \\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$";
                    break;
                case 18:
                    //只能輸入中文
                    RegularExpressions = "^[^\u4E00-\u9FA5]";
                    break;
                case 19:
                    //只能输入0和非0打头的数字
                    RegularExpressions = "^(0|[1-9][0-9]*)$";
                    break;
                case 20:
                    //只能输入数字
                    RegularExpressions = "^[0-9]*$";
                    break;
                case 21:
                    //只能輸入數字加2位小數
                    RegularExpressions = "^[0-9]+(.[0-9]{1,2})?$";
                    break;
                case 22:
                    //只能输入0和非0打头的数字加2位小數
                    RegularExpressions = "^(0|[1-9]+)(.[0-9]{1,2})?$";
                    break;
                case 23:
                    //只能输入0和非0打头的数字加2位小數  但不匹配0.00
                    RegularExpressions = "^(0(.(0[1-9]|[1-9][0-9]))?|[1-9]+(.[0-9]{1,2})?)$";
                    break;
                default:
                    break;
            }

            Match m = Regex.Match(_value, RegularExpressions);

            if (m.Success)
                return true;
            else
                return false;

        }
        #endregion 

    申明

    非源创博文中的内容均收集自网上,若有侵权之处,请及时联络,我会在第一时间内删除.再次说声抱歉!!!

    博文欢迎转载,但请给出原文连接。

  • 相关阅读:
    Ubuntu 12.10下为Firefox安装Adobe Flash Player
    oracle同时操作两个实例,如何进行切换操作?
    Ubuntu 12.10下为Firefox安装Adobe Flash Player
    Ubuntu11.10 安装oracle 11g
    oracle忘记system和sys账户的密码怎么办?
    linux 搭建java开发环境
    Oracle 笔记 day04
    节约网络带宽的设计策略
    集群化服务器
    01 基础概念(实战Linux Shell编程与服务器管理)
  • 原文地址:https://www.cnblogs.com/Athrun/p/1022678.html
Copyright © 2020-2023  润新知