新闻资讯

在C#.Net中判斷輸入的字串是否是數字的方法

发布时间:2013-08-22

点击量:

在Textbox的輸入中﹐我們常常需要控制輸入的類型﹐比如說只能輸入數字﹐當然實現的方法很多﹐我總結了一下我做過的一些項目﹐我常會使用以下這三種﹕
1﹑使用Try...Catch
        private static bool IsNumeric(string itemValue,int intFLag)
        {
            try
            {
                int i = Convert.ToInt32(itemValue);
                return true;
             }
            catch
            {
                return false;
             }
         }

2﹑使用正則表達式
using System.Text.RegularExpressions;
        
         private static bool IsNumeric(string itemValue)
        {
            return (IsRegEx("^(-?[0-9]×[.]×[0-9]{0,3})$", itemValue));
         }

        private static bool IsRegEx(string regExValue, string itemValue)
        {
            try
            {
                 Regex regex = new System.Text.RegularExpressions.Regex(regExValue);
                if (regex.IsMatch(itemValue)) return true;
                else                          return false;
             }
            catch (Exception )
            {
                return false;
             }
            finally
            {
             }
         }

3﹑判斷輸入的keyCode
      


服务热线

服务热线

15383239821

微信咨询
微信二维码
返回顶部
×微信二维码

截屏,微信识别二维码

微信号:

(点击微信号复制,添加好友)

打开微信

微信号已复制,请打开微信添加咨询详情!