在C#.net中使用正则表达式检验输入是否为数字

80酷酷网    80kuku.com

  正则直接调用以下函数进行检验,返回值为true表示输入是数字,反之不是。
itemValue为输入的值。

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




分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: