日期:2014-05-18  浏览次数:20368 次

验证输入内容为数值型
有个textbox 想用验证控件验证输入内容为数值型

------解决方案--------------------
private 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; 
}

------解决方案--------------------
正则 \d
------解决方案--------------------
IsDigit
------解决方案--------------------
<asp:RegularExpressionValidator id="RegularExpressionValidator1" 
ControlToValidate="TextBox1"
ValidationExpression="\d"
Display="Static"
EnableClientScript="false"
ErrorMessage="必须为数字"
runat="server"/>

------解决方案--------------------
探讨
private 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;
}

------解决方案--------------------
探讨
<asp:RegularExpressionValidator id="RegularExpressionValidator1"
ControlToValidate="TextBox1"
ValidationExpression="\d"
Display="Static"
EnableClientScript="false"
ErrorMessage="必须为数字"
runat="server"/>

------解决方案--------------------
<asp:RegularExpressionValidator id="RegularExpressionValidator1" 
ControlToValidate="TextBox1" 
ValidationExpression="\d" 
Display="Static" 
EnableClientScript="false" 
ErrorMessage="必须为数字" 
runat="server"/> 

------解决方案--------------------
呵呵。。大家的都不错。。我来一个高效的。
public bool IsNumber(string txt)
{
bool returnVal = false;
try
{
int.Parse(txt);//不是整数是报异常
returnVal = true;
}
catch
{}
return returnVal;
}
------解决方案--------------------
<asp:RegularExpressionValidator id="RegularExpressionValidator1" 
ControlToValidate="TextBox1" 
ValidationExpression="\d" 
Display="Static" 
EnableClientScript="false" 
ErrorMessage="必须为数字" 
runat="server"/> 

------解决方案--------------------
public bool IsNumber(string txt)
{
bool returnVal = false;
try
{
int.Parse(txt);//不是整数是报异常
returnVal = true;
}
catch
{}