如何验证用户输入的是否是5位数字信息?
或者是3位、4位

解决方案 »

  1.   

    if(验证的string.lenth==5)
             {
                int RS;
                if (int.TryParse(验证的string, out RS))
                {
                    //todo
                }
             }
      

  2.   

    1.对安全要求不太高,在客户端验证
     用RegularExpressionValidator控件
    2.
     如果要求安全,在服务端验证:
      //判断是不是数字类型
    public bool JudgeFigure(string str)
    {
    if(str.Trim().Length<=0)
    return true;
    int dot=0;
    if(str[0]=='.'||str[str.Length-1]=='.')
    return false;
    for(int i=0;i<str.Length;i++)
    {
    if(dot>1) return false;
    if(Char.IsDigit(str,i))
    {
    continue;
    }
    if(str[i]=='.')
    {
    dot=dot+1;
    continue;
    }
    return false;
    }
    return true;
    }
     
     
      

  3.   

    RegularExpressionValidator就可以了
      

  4.   

    RegularExpressionValidator验证控件^\d{5}$想允许几位中间数字替换成几
    如果允许3~5位
    ^\d{3,5}$
      

  5.   

    Regex reg = new Regex("[0-9]{3,5}");if(!reg.IsMatch(strNum))
    {
       //错误处理
    }