TextBox只能输入数字的最简洁方法!
如下TextBox
<asp:textbox onkeypress="isnum()" id="TextBox1" runat="server"></asp:textbox>
1. 在页面中添加一段脚本:
2. 在代码中添加:
就ok了,是不是很简洁啊? ^_^
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
1. 在页面中添加一段脚本:
<script language="javascript">
function isnum()
{
if(event.keyCode<45 || event.keyCode>57)
{
event.keyCode=0;
}
}
</script>
function isnum()
{
if(event.keyCode<45 || event.keyCode>57)
{
event.keyCode=0;
}
}
</script>
2. 在代码中添加:
try
{
Convert.ToDouble(TextBox1.Text.Trim());
}
catch
{
Response.Write("<script>alert('请输入正确的数字格式!');</script>");
TextBox1.Text="";
Page.RegisterStartupScript("focus","<script>document.forms[0]['TextBox1'].focus();</script>");
}
{
Convert.ToDouble(TextBox1.Text.Trim());
}
catch
{
Response.Write("<script>alert('请输入正确的数字格式!');</script>");
TextBox1.Text="";
Page.RegisterStartupScript("focus","<script>document.forms[0]['TextBox1'].focus();</script>");
}
就ok了,是不是很简洁啊? ^_^