求正则表达式:只能输入数字或者为空

解决方案 »

  1.   

    如果只是限制文本框的输入值的话.用KeyPress事件吧.
      

  2.   

    private void textBox2_KeyPress(object sender, KeyPressEventArgs e) 
            { 
                //检测是否已经输入了小数点 
                bool IsContainsDot = this.textBox2 .Text.Contains("."); 
                if ((e.KeyChar < 48 || e.KeyChar > 57) && (e.KeyChar != 8) && (e.KeyChar != 46)) 
                { 
                    e.Handled = true; 
                } 
                else if (IsContainsDot && (e.KeyChar == 46)) //如果输入了小数点,并且再次输入 
                { 
                    e.Handled = true; 
                } 
        
            }
      

  3.   

    function IsNum(num)
    {
     if(num =null || IsNum(num))
    {
     return true;
    }
    else
    {
      return false;
    }}