验证decimal(4,2),decimal(5,2),decimal(2,1),int类型,字符串长度等。
//验证是否为dec(5,2)小数 
        public bool IsValidDecimal5_2(string strIn)
        {            //return Regex.IsMatch(strIn, @"[0].\d{1,2}|[1]");
            return Regex.IsMatch(strIn, @"[0-150].\d{0,2}}");
        }        //验证是否为dec(4,2)小数 
        public bool IsValidDecimal4_2(string strIn)
        {            //return Regex.IsMatch(strIn, @"[0].\d{1,2}|[1]");
            return Regex.IsMatch(strIn, @"[0-99].\d{0,2}");
        }        //验证是否为dec(4,2)小数 
        public bool IsValidDecimal2_1(string strIn)
        {            //return Regex.IsMatch(strIn, @"[0].\d{1,2}|[1]");
            return Regex.IsMatch(strIn, @"[0-9].\d{0,1}");
        }        //验证是否为int类型整数 
        public bool IsValidInt(string strIn)
        {            //return Regex.IsMatch(strIn, @"[0].\d{1,2}|[1]");
            return Regex.IsMatch(strIn, @"\d");
        }
就一个decimal(2,1)的好用,搞不懂了,求教~~

解决方案 »

  1.   

    //验证是否为dec(5,2)小数 
            public bool IsValidDecimal5_2(string strIn)
            {
               return Regex.IsMatch(strIn, @"\d{5}\.\d{2}");
            }        //验证是否为dec(4,2)小数 
            public bool IsValidDecimal4_2(string strIn)
            {
               return Regex.IsMatch(strIn, @"\d{4}\.\d{2}");
            }        //验证是否为int类型整数 
            public bool IsValidInt(string strIn)
            {
                return Regex.IsMatch(strIn, @"\d+");
            }
      

  2.   

    楼上的,先谢谢你的热心回答,但是你写出的正则根本就不对,比如dec(5,2),数字的位数最多是5位,其中整数部分1~3位,小数部分0~2位,你的正则连最标准的123.45都校验不过,继续求教
      

  3.   

    //验证是否为dec(5,2)小数 
            public bool IsValidDecimal5_2(string strIn) 
            { 
              return Regex.IsMatch(strIn, @"\d{1,3}\.\d{0,2}"); 
            }         //验证是否为dec(4,2)小数 
            public bool IsValidDecimal4_2(string strIn) 
            { 
              return Regex.IsMatch(strIn, @"\d{1,2}\.\d{0,2}"); 
            }         //验证是否为int类型整数 
            public bool IsValidInt(string strIn) 
            { 
                return Regex.IsMatch(strIn, @"\d+"); 
            } 
      

  4.   


            //验证是否为dec(5,2)小数 
            public bool IsValidDecimal5_2(string strIn)
            {
                return Regex.IsMatch(strIn, @"^[+-]?(?=([0-9]{1,5}$|[0-9.]{3,6}$))(0|[1-9][0-9]{0,4})(\.[0-9]{1,2})?$",RegexOptions.ExplicitCapture);
            }        //验证是否为dec(4,2)小数 
            public bool IsValidDecimal4_2(string strIn)
            {
                return Regex.IsMatch(strIn, @"^[+-]?(?=([0-9]{1,4}$|[0-9.]{3,5}$))(0|[1-9][0-9]{0,3})(\.[0-9]{1,2})?$", RegexOptions.ExplicitCapture);
            }        //验证是否为dec(2,1)小数 
            public bool IsValidDecimal2_1(string strIn)
            {
                return Regex.IsMatch(strIn, @"^(?=([0-9]{1,2}$|[0-9.]{3}$))(0|[1-9][0-9]{0,1})(\.[0-9])?$", RegexOptions.ExplicitCapture);
            }        //验证是否为int类型整数 
            public bool IsValidInt(string strIn)
            {
                return Regex.IsMatch(strIn, @"^[+-]?(0|[1-9][0-9]*)$", RegexOptions.ExplicitCapture);
            }
      

  5.   

    Zhanlixin你好,谢谢你的热心回答,你写的正则之前我也这么写过,可是看似理论上正确了,却有很多的错误,可以用RegexTester来测试一下。具体我也不知道为什么...
    还是得谢谢你的热心帮助