在C#.net中使用正則表達式檢驗輸入是否爲數字

在C#.net中使用正則表達式檢驗輸入是否爲數字
 


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
    {
    }
   }

 

 

 

上面這個其實不對

 

if (!System.Text.RegularExpressions.Regex.IsMatch(temp, @"^[-]?/d+[.]?/d*$"))

 

這個也許差不多

發佈了27 篇原創文章 · 獲贊 0 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章