• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
icestream
博客园    首页    新随笔    联系   管理    订阅  订阅

辛辛苦苦整合的TextArea设置MaxLength的代码

Javascript代码

 

view plaincopy to clipboardprint?
  1. function SetTextAreaMaxLength(controlId,length)   
  2. {   
  3.     // JScript File for TextArea   
  4.     // Keep user from entering more than maxLength characters   
  5.     function doKeypress(control,length){   
  6.         maxLength = length;   
  7.         value = control.value;   
  8.         if(maxLength && value.length > maxLength-1){   
  9.             event.returnValue = false;   
  10.             maxLength = parseInt(maxLength);   
  11.         }   
  12.     }   
  13.     // Cancel default behavior   
  14.     function doBeforePaste(control,length){   
  15.         maxLength = length;   
  16.         if(maxLength)   
  17.         {   
  18.             event.returnValue = false;   
  19.         }   
  20.     }   
  21.     // Cancel default behavior and create a new paste routine   
  22.     function doPaste(control,length){   
  23.         maxLength = length;   
  24.         value = control.value;   
  25.         if(maxLength){   
  26.             event.returnValue = false;   
  27.             maxLength = parseInt(maxLength);   
  28.             var oTR = control.document.selection.createRange();   
  29.             var iInsertLength = maxLength - value.length + oTR.text.length;   
  30.             var sData = window.clipboardData.getData("Text").substr(0,iInsertLength);   
  31.             oTR.text = sData;   
  32.         }   
  33.     }   
  34.        
  35.     function doDragenter(control,length)   
  36.     {   
  37.         maxLength = length;   
  38.         value = control.value;   
  39.         if(maxLength){   
  40.             event.returnValue = false;   
  41.         }      
  42.     }   
  43.   
  44.        
  45.     function addEvent(elm, evType, fn, useCapture)   
  46.     {    
  47.         if (elm.addEventListener)   
  48.         {    
  49.             elm.addEventListener(evType, fn, useCapture);    
  50.             return true;    
  51.         }    
  52.         else if (elm.attachEvent)    
  53.         {    
  54.             var r = elm.attachEvent('on' + evType, fn);    
  55.             return r;    
  56.         }    
  57.         else {    
  58.             elm['on' + evType] = fn;    
  59.         }    
  60.     }   
  61.   
  62.   
  63.     function AttacheventTextAreaBeforePaste(obj,length)   
  64.     {   
  65.         return function()   
  66.         {   
  67.             doBeforePaste(obj,length)   
  68.         }   
  69.     }   
  70.        
  71.     function AttacheventTextAreaPaste(obj,length)   
  72.     {   
  73.         return function()   
  74.         {   
  75.             doPaste(obj,length)   
  76.         }   
  77.     }   
  78.        
  79.     function AttacheventTextAreaKeyPress(obj,length)   
  80.     {   
  81.         return function()   
  82.         {   
  83.             doKeypress(obj,length)   
  84.         }   
  85.            
  86.     }   
  87.        
  88.     function AttacheventTextAreaDragEnter(obj,length)   
  89.     {   
  90.         return function()   
  91.         {   
  92.             doDragenter(obj,length);   
  93.         }   
  94.     }   
  95.        
  96.        
  97.     var obj = document.getElementById(controlId);   
  98.        
  99.     addEvent(obj,'keypress',AttacheventTextAreaKeyPress(obj,length),null);   
  100.     addEvent(obj,'beforepaste',AttacheventTextAreaBeforePaste(obj,length),null);   
  101.     addEvent(obj,'paste',AttacheventTextAreaPaste(obj,length),null);   
  102.     addEvent(obj,'dragenter',AttacheventTextAreaDragEnter(obj,length),null);    
  103. }  

 

HTML代码

 

view plaincopy to clipboardprint?
  1. <asp:TextBox ID="TextBoxAddress" runat="server" Width="200px"    
  2.                         TextMode="MultiLine" Height="113px" MaxLength="10"></asp:TextBox>  
  3.   
  4.     <script language="javascript" type="text/javascript">  
  5.     SetTextAreaMaxLength('<%=TextBoxAddress.ClientID %>',10);   
  6.        
  7. </script>  

 

该javascript控制了输入,粘贴,拖放操作

posted @ 2010-01-19 09:21  icestream  阅读(810)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3