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

来源:互联网 发布:网络教育石油大学好吗? 编辑:程序博客网 时间:2024/05/17 22:08
第一种方法:正则表达式<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中貌似没有。
但部分书上有这么写。
等有准确的消息在来修改。
                                             
0 0
原创粉丝点击