您的位置:首页 > 编程语言

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

2014-11-18 15:24 162 查看
第一种方法:正则表达式<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中貌似没有。

但部分书上有这么写。

等有准确的消息在来修改。


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: