验证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.   

    decimal(4,2)
    /\w{4}.\d{2}/decimal(5,2)
    /\w{5}.\d{2}/decimal(2,1)
    /\w{2}.\d{1}/int
    /\d+/
      

  2.   

    说一下我的具体需求吧,数据库的类型如上几种,在输入的时候只要不违反数据库类型,插入数据库的时候不报错都可以输入。
    比如:decimal(5,2)类型,我的整数部分可以录入0~3位数,不能录入3位以上,小数部分可以录入0~2位,不能录入2位以上 
    /\w{4}.\d{2}|\d{0,3} 这个是我针对楼上大哥写的和我的具体需求改写的一个例子,但这样写如果输入1234又会通过校验...
    也就是输入的数字位数没有限制了,继续请教
      

  3.   


            //验证是否为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);
            }
      

  4.   

    /\w{4}.\d{2}|\d{0,3} 这个是我针对楼上大哥写的和我的具体需求改写的一个例子,但这样写如果输入1234又会通过校验... 
    也就是输入的数字位数没有限制了这句基本没看明白,你这个表达式是要验证什么样的数据?几位整数几位小数?
      

  5.   

    这么说吧,
    \d 表示匹配0-9
    \d{1,2} 表示匹配0-9,且至少一位,至多两位
    \d{n,m} 表示匹配0-9,且至少n位,至多m位
    .表示任意字符
    \.表示.本身(逗点)
      

  6.   

    楼上大哥说的这些我都明白,这么说吧
    decimal(5,2)类型,整数部分可以录入1~3位数,超过3位插入数据库报错,小数部分可以录入0~2位,不能录入2位以上 
    根据3楼大哥的回答修改过后我所要的正解如下:
    ^(?=([0-9]{1,3}$|[0-9.]{3,6}$))(0|[1-9][0-9]{0,2})(\.[0-9]{1,2})?$