1.只能输入数字和小数点的文本框: <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Untitled Page</title> <script language="JavaScript" type="text/javascript"> function clearNoNum(obj) { //先把非数字的都替换掉,除了数字和. obj.value = obj.value.replace(/[^\d.]/g,""); //必须保证第一个为数字而不是. obj.value = obj.value.replace(/^\./g,""); //保证只有出现一个.而没有多个. obj.value = obj.value.replace(/\.{2,}/g,"."); //保证.只出现一次,而不能出现两次以上 obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$","."); } </script> </head> <body> <form id="form1" runat="server"> <div> 只能输入数字和小数点的文本框: <input id="input1" onkeyup="clearNoNum(this)" /> <asp:TextBox ID="TextBox1" onkeyup="clearNoNum(this)" runat="server"></asp:TextBox> </div> </form> </body> </html> 2.只能输入数字的文本框: <asp:TextBox ID="Txtname" runat="server" onkeyup="this.value=this.value.replace(/\D/g,'')"></asp:TextBox> 3. 提交时对输入文本框中的内容进行数字校验: function CheckNum(strTemp) { if (isNaN(strTemp)) { alert("包含有非数字字符!"); return false; } else { var n = parseInt(strTemp); if (n.toString() != strTemp) { alert("不是一个整数!"); return false; } } }