一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

判断用户输入为数字函数

时间:2011-02-13 编辑:简简单单 来源:一聚教程网

判断用户输入为数字函数

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("不是数字"); }
    }

热门栏目