TextBox只能输入数字的最简洁方法!

如下TextBox
<asp:textbox onkeypress="isnum()" id="TextBox1" runat="server"></asp:textbox>

1. 在页面中添加一段脚本:
<script language="javascript">
    
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>");
}

就ok了,是不是很简洁啊? ^_^
posted @ 2007-04-20 12:14  tohen  阅读(4087)  评论(5编辑  收藏  举报