.net编程时 判断是否输入为数字

第一种方法:正则表达式<span style="white-space:pre">	</span>
Regex numRegex = new Regex(@"^\d+$");
            string id = Request.QueryString["Id"];
            if (numRegex.IsMatch(id))
            {
                // id 为纯数字。。
            }
            else
            {
                // id 包含数字以外的字符。。
            }


第二种方法:
try catch方法
例:
     try
     {
       Convert.ToInt32("123"):
       Console.Write("是数字");
     }
     catch(Exception ex)
     {
       Console.Write("非数字");
     }
第三种方法:
Information.IsNumeric(txt_num.Text.Trim());
但是Information在4.5中貌似没有。
但部分书上有这么写。
等有准确的消息在来修改。

                            

你可能感兴趣的:(.net)