单纯的判断是否是正整数,可使用char.IsDigh(string,int index)和IsNumber(string,int index)函数

protected void Button2_Click(object sender, EventArgs e)
    {
        //判断正整数
        int j=0;
        for (int i = 0; i < TextBox1.Text.Length; i++)
        {
            if (char.IsNumber(TextBox1.Text, i))//这个方法用来判断整数还可以,判断负数和小数就失效了
                j++;
        }
        if (j == TextBox1.Text.Length)
        {
            Response.Write("ok");
        }
        else
        {  Response.Write ("no");}
      
    }

但是,出现负数或者小数的时候,以上方法失效,则,使用自定义功能函数

public bool  IsNumber( object obj)
  {
    bool   result   =   true;
    try
      {
          string   str   =   obj.ToString();
          double   d   ; 
          d   =   double.Parse(str);
      }
    catch
      {  //parse 函数进行转换,不成功则抛出异常
          result   =   false;
      }
    return   result;

  }
    protected void Button3_Click1(object sender, EventArgs e)
    {
        //判断数

        if (IsNumber(TextBox1.Text))
        {
            Response.Write("是数字");
        }
        else
        { Response.Write("不是数字"); }
    }

第三种方法是 使用正则表达式,以后学习。

posted on 2011-02-13 00:10  lovechenxiao  阅读(6414)  评论(2编辑  收藏  举报