写一个字符串判断函数吧,检索字符串中的每一个字符,若ascii码不属于0-9就报错

解决方案 »

  1.   


    private void txtTest_KeyPress(object sender, KeyPressEventArgs e)
            {
                if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 13)  //按下的键不是0-9也不是退格和回车
                {
                    //do nothing
                    e.Handled = true;
                }
    }
      

  2.   

    正则也行啊if(!Regex.match(textbox1.text,"^[0-9]+$").Success)
      

  3.   

    正则string pattern="^[0-9]+$";
      

  4.   

    Int32 j;
    if (Int32.TryParse(textBox1.Text,out j))
    {
      // textBox1.Text 可轉為 Int32
    }
    else
    {
      // textBox1.Text 無法轉為 Int32
    }if (i == j)
    {}
      

  5.   

     public static bool IsPhone(string input)
            {
                string pattern = @"^[0-9]+$";
                Regex regex = new Regex(pattern);
                return regex.IsMatch(input);
            }
      

  6.   

    使用验证控件RegularExpressionValidator控件  
      

  7.   

    呜呜呜我还是弄不上啊,我的那个文本框叫txttel在if 里面怎么写啊 ?
    用这个的时候提示没有Regex,这个还要先定义?if(!Regex.match(textbox1.text,"^[0-9]+$").Success)
      

  8.   


      private void txtPageIndex_KeyPress(object sender, KeyPressEventArgs e)
            {            if (!(Char.IsNumber(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == (char)Keys.Enter))
                {
                    e.Handled = true; // 只允许数字键 回格 和 回车
                }
            }