[转]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>

 

posted on 2016-03-07 10:12  freeliver54  阅读(301)  评论(1编辑  收藏  举报

导航