zoukankan      html  css  js  c++  java
  • [转]OnKeyDown Numeric Validator CLIENT SIDE

    本文转自:http://forums.asp.net/t/1211724.aspx?OnKeyDown+Numeric+Validator+CLIENT+SIDE

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
      <title>Numbers Only Textbox by Josh Stodola</title>
      <script type="text/javascript">
        function numbersOnly(e) {
          var evt = (e) ? e : window.event;
          var key = (evt.keyCode) ? evt.keyCode : evt.which;
    
    
          if(key != null) {
            key = parseInt(key, 10);
    
    
            if((key < 48 || key > 57) && (key < 96 || key > 105)) {
              if(!isUserFriendlyChar(key))
                return false;
            }
            else {
              if(evt.shiftKey)
                return false;
            }
          }
    
    
          return true;
        }
    
    
        function isUserFriendlyChar(val) {
          // Backspace, Tab, Enter, Insert, and Delete
          if(val == 8 || val == 9 || val == 13 || val == 45 || val == 46)
            return true;
    
    
          // Ctrl, Alt, CapsLock, Home, End, and Arrows
          if((val > 16 && val < 21) || (val > 34 && val < 41))
            return true;
    
    
          // The rest
          return false;
        }
      </script>
    </head>
    <body>
      <h1>Numbers Only Textbox by Josh Stodola</h1>
      <input type="text" onkeydown="return numbersOnly(event);" onpaste="return false;" />
    </body>
    </html>

    强化后:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
      <title>Numbers Only Textbox by Josh Stodola</title>
      <script type="text/javascript">
        function numbersOnly(obj,e,afterPointLength) {
          if (typeof(afterPointLength)==='undefined') afterPointLength = 3;
            
          var evt = (e) ? e : window.event;
          var key = (evt.keyCode) ? evt.keyCode : evt.which;
          var value = obj.value;
    
          if ((value.indexOf('.') != -1))
          {
            // alert(value.substring(value.indexOf('.')).length );
          }
         
          
          if(key != null) {
            key = parseInt(key, 10);
    
            //只能输入一个小数点
            if( (key == 110 && value.indexOf(".") < 0)
                || (key == 190 && value.indexOf(".") < 0) ){
                    //alert(value.length);
                    //第一位不能是小数点
                    if(value.length == 0){
                        return false;
                    }else{
                        return true;
                    }
                }
        
                
            if((key < 48 || key > 57) && (key < 96 || key > 105)) {
              if(!isUserFriendlyChar(key)){
                return false;
              }
                
            }
            else {
              if(evt.shiftKey)
                return false;
            }
          }
    
          //小数点后2位
        if ((value.indexOf('.') != -1)){
            if (value.substring(value.indexOf('.')).length > afterPointLength) 
            // &&    (event.which != 0 && event.which != 8) 
            // &&    ($(this)[0].selectionStart >= text.length - 2)
            {
            if(!isUserFriendlyChar(key)){
                return false;
              }
            }
        }
    
          return true;
        }
    
    
        function isUserFriendlyChar(val) {
          // Backspace, Tab, Enter, Insert, and Delete
          if(val == 8 || val == 9 || val == 13 || val == 45 || val == 46)
            return true;
    
    
          // Ctrl, Alt, CapsLock, Home, End, and Arrows
          if((val > 16 && val < 21) || (val > 34 && val < 41))
            return true;
    
    
          // The rest
          return false;
        }
      </script>
    </head>
    <body>
      <h1>Numbers Only Textbox by Josh Stodola</h1>
      <input type="text" onkeydown="return numbersOnly(this,event);" onpaste="return false;" />
    </body>
    </html>
  • 相关阅读:
    js循环
    js对象
    实现checkebox全选取消操作
    js数组
    jquery记忆笔记
    js选择checkbox值,组织成key-value形式,传值到后台
    ES6常用语法,面试应急专用!
    Win10 系统运行VsCode出现白屏的问题(亲测有效)
    command failed: npm install --loglevel error --registry=https://registry.npm 用vue-cli 4.0 新建项目总是报错
    MODULE BUILD FAILED: ERROR: COULDN’T FIND PRESET “ES2015” RELATIVE TO DIRECTORY
  • 原文地址:https://www.cnblogs.com/freeliver54/p/5249593.html
Copyright © 2011-2022 走看看