在接收一个数据时,如ID号,为了防止提交者自行更改该数值,加如下判断
if (Request.QuseryString["ID"]!=null)
{
........
}
可如果浏览者直接把例如ID=256,后面的256给去掉后,上面的方法就检查不出来了,给出个错误,我想在判断时检查一下取到的数值类型,判断它是哪种类型的,然后进行相应操作,该怎么做?
if (判断是不是数字型)
{
........
}
上面该怎么写呢???
if (Request.QuseryString["ID"]!=null)
{
........
}
可如果浏览者直接把例如ID=256,后面的256给去掉后,上面的方法就检查不出来了,给出个错误,我想在判断时检查一下取到的数值类型,判断它是哪种类型的,然后进行相应操作,该怎么做?
if (判断是不是数字型)
{
........
}
上面该怎么写呢???
public bool IsInt(string input)
{
try{
int.Parse(input);
return true;
}
catch{return false;}
}
{
........
}
string id = "";
if ( Request["ID"] != null )
{
id = Request["ID"].ToString();
}1.使用try……catch看看转化时是否出现异常
try
{
int number = int.Parse( id );
// int number = System.Convert.ToInt32( id );
}catch
{
}
2.使用正则表达式来检测数字是否为整数
if ( Regex.IsMatch( id , @"^[1-9]\d+|\d" ) )
{
Console.WriteLine( "ID is a number" );
}