作者:bluesky35(蓝天)
直接调用以下函数进行检验,返回值为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
{
}
}
博客给出了检验输入是否为数字的方法,通过直接调用函数实现。利用 System.Text.RegularExpressions 命名空间,定义了 IsNumeric 和 IsRegEx 函数,在 IsRegEx 函数中使用正则表达式匹配输入值,若匹配则返回 true,反之返回 false,同时对异常进行了处理。

1518

被折叠的 条评论
为什么被折叠?



