现在的位置: 首页 > 综合 > 正文

C#验证输入的是否数字的方法

2013年04月13日 ⁄ 综合 ⁄ 共 371字 ⁄ 字号 评论关闭

static bool IsNumeric(string str)
  {
   if (str==null || str.Length==0)
    return false;
   foreach(char c in str)
   {
    if (!Char.IsNumber(c))
    {
     return false;
    }
   }
   return true;
  }

正则表达的写法是:

static bool IsNumeric(string str) 
{  
   System.Text.RegularExpressions.Regex reg1 
       = new System.Text.RegularExpressions.Regex(@"^[-]?/d+[.]?/d*$");  
  
return reg1.IsMatch(str); 
}
 

抱歉!评论已关闭.