js只允许输入数字和小数点 .

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4.     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>  
  5.     <script type="text/javascript">  
  6.         //示例代码:   
  7.         //只允许输入数字与.:<input type="text" name="test" id="test" onkeydown="checkKeyForFloat(this.value,event)" style="ime-mode: disabled" />   
  8.         //只允许输入数字   :<input type="text" name="test2" id="test2" onkeydown="checkKeyForNum(this.value,event)" style="ime-mode: disabled" />   
  9.         //只允许输入数字与小数点     
  10.         function checkKeyForFloat(value, e) {  
  11.             var isOK = false;  
  12.             var key = window.event ? e.keyCode : e.which;  
  13.             if ((key > 95 && key < 106) ||                  //小键盘上的0到9   
  14.             (key > 47 && key < 60) ||                   //大键盘上的0到9   
  15.             (key == 110 && value.indexOf(".") < 0) ||   //小键盘上的.而且以前没有输入.   
  16.             (key == 190 && value.indexOf(".") < 0) ||   //大键盘上的.而且以前没有输入.   
  17.             key == 8 || key == 9 || key == 46 || key == 37 || key == 39     //不影响正常编辑键的使用(8:BackSpace;9:Tab;46:Delete;37:Left;39:Right)   
  18.         ) {  
  19.                 isOK = true;  
  20.             } else {  
  21.                 if (window.event) //IE     
  22.                 {  
  23.                     e.returnValue = false;   //event.returnValue=false 效果相同.     
  24.                 }  
  25.                 else //Firefox     
  26.                 {  
  27.                     e.preventDefault();  
  28.                 }  
  29.             }  
  30.             return isOK;  
  31.         }  
  32.   
  33.         //只允许输入数字     
  34.         function checkKeyForInt(value, e) {  
  35.             var isOK = false;  
  36.             var key = window.event ? e.keyCode : e.which;  
  37.             if ((key > 95 && key < 106) ||                  //小键盘上的0到9   
  38.             (key > 47 && key < 60) ||                   //大键盘上的0到9   
  39.             key == 8 || key == 9 || key == 46 || key == 37 || key == 39     //不影响正常编辑键的使用(8:BackSpace;9:Tab;46:Delete;37:Left;39:Right)   
  40.         ) {  
  41.                 isOK = true;  
  42.             } else {  
  43.                 if (window.event) //IE     
  44.                 {  
  45.                     e.returnValue = false;   //event.returnValue=false 效果相同.     
  46.                 }  
  47.                 else //Firefox     
  48.                 {  
  49.                     e.preventDefault();  
  50.                 }  
  51.             }  
  52.             return isOK;  
  53.         }  
  54.   
  55.         //设置有自定义属性 dtype 的文本框 允许输入的范围   
  56.         function setDType() {  
  57.             $(":text[dtype]").each(function () {  
  58.                 var dtype = $(this).attr("dtype");  
  59.                 var isOK = true;  
  60.                 switch (dtype) {  
  61.                     case "number":  
  62.                         $(this).css("ime-mode""disabled").keydown(function (event) {  
  63.                             isOK = checkKeyForFloat($(this).val(), event);  
  64.                             if (!isOK) {  
  65.                                 //$(this).SuperFocus("", 500);   
  66.                             }  
  67.                             return isOK;  
  68.                         });  
  69.                         break;  
  70.                     default:  
  71.                         break;  
  72.                 }  
  73.             });  
  74.         }  
  75.     </script>  
  76.     <script type="text/javascript">  
  77.         $(function () {  
  78.             setDType();  
  79.         });  
  80.     </script>  
  81. </head>  
  82. <body>  
  83.     年龄: <input type="text" maxlength="3" onkeydown="checkKeyForInt(this.value,event)" style="ime-mode: disabled"/><br />  
  84.     身高:<input type="text" maxlength="5" dtype="number" />  
  85. </body>  
  86. </html>  
posted @ 2013-08-14 10:30  namehwh  阅读(710)  评论(0编辑  收藏  举报