只能输入0-9多包括小数点

  1. <html>
  2. <head>
  3. <meta http-equiv="content-Type" content="text/html;charset=gb2312">
  4. <title>js 只能输入数字和小数点</title>
  5. <script language="JavaScript" type="text/javascript">
  6. function clearNoNum(obj)
  7. {
  8.    obj.value = obj.value.replace(/[^\d.]/g,"");  //清除“数字”和“.”以外的字符
  9.    obj.value = obj.value.replace(/^\./g,"");  //验证第一个字符是数字而不是.
  10.    obj.value = obj.value.replace(/\.{2,}/g,"."); //只保留第一个. 清除多余的.
  11.    obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
  12. }
  13. </script>
  14. </head>
  15. <body>
  16. 只能输入数字和小数点的文本框:<input name="input1" onkeyup="clearNoNum(this)">
  17. </body>
  18. </html>

 posted on 2011-01-21 10:44  纳米程序员  阅读(8921)  评论(0编辑  收藏  举报