- JS与.net前后台互访
- C#异步方法async/await的三种返回类型
- .net如何将用kindeditor将数据上传到数据库
- Ecshop静态生成网站解决方案
- 301设置方法和转向代码
- 使用Global.asax的Application_Error记录Exception信息
- 一些应该熟记于心的jQuery函数和技巧
- fckeditor在ie9中不支持$=document.getElementById解决办法
- 用CSS+JS控制Li背景颜色交替
- WebP图片:网页优化的轻量化利器
邮箱:
手机:15383239821
在C#.Net中判斷輸入的字串是否是數字的方法
在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
public static bool IsNumeric(System.Windows.Forms.KeyPressEventArgs e)

{
if ((e.KeyChar >= (char)48 && e.KeyChar<=(char)57) ||
e.KeyChar ==(char)8 || e.KeyChar ==(char)45 || e.KeyChar ==(char)47)

{
}
else

{
e.Handled=true;
}
return true;
}
public static bool isNumeric(string strInput)
{
char[] ca = strInput.ToCharArray();
bool found = true;
for (int i = 0; i < ca.Length; i++)
{
if ((ca[i] < '0' || ca[i] > '9') && ca[i] != '.')
{
found = false;
break;
};
};
return found;
}
- 上一篇:从客户端中检测到有潜在危险的 Request.Form 值
- 下一篇:正则表达式判断
-
2023-09-13新站要多发原创内容
-
2010-12-09PageRank(网页级别)是什么意思?
-
2012-10-14不能使用 ';文件已在使用中
-
2013-05-01如何使用VS2008打开VS2010的解决方案
-
2010-11-11新站搜索引擎SEO优化的四个忌讳
-
2013-11-12解决asp.net中“从客户端中检测到有潜在危险的Request.Form值”的错误
