1. 程式人生 > >使用正則驗證非零的正整數

使用正則驗證非零的正整數

實現效果:

  

實現原理:

  

 

實現程式碼:

        private void button1_Click(object sender, EventArgs e)
        {
            if (System.Text.RegularExpressions.Regex.
                IsMatch(textBox1.Text.ToString(), @"^\+?[1-9][0-9]*$"))
            {
                label2.Text = "恭喜 通過";
            }
            else { label2.Text="很遺憾 沒有";}
        }
        //同理驗證非零的負整數:@"^\-[1-9][0-9]*$"