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

Javascript代码

 

  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. }  
function SetTextAreaMaxLength(controlId,length) { // JScript File for TextArea // Keep user from entering more than maxLength characters function doKeypress(control,length){ maxLength = length; value = control.value; if(maxLength && value.length > maxLength-1){ event.returnValue = false; maxLength = parseInt(maxLength); } } // Cancel default behavior function doBeforePaste(control,length){ maxLength = length; if(maxLength) { event.returnValue = false; } } // Cancel default behavior and create a new paste routine function doPaste(control,length){ maxLength = length; value = control.value; if(maxLength){ event.returnValue = false; maxLength = parseInt(maxLength); var oTR = control.document.selection.createRange(); var iInsertLength = maxLength - value.length + oTR.text.length; var sData = window.clipboardData.getData("Text").substr(0,iInsertLength); oTR.text = sData; } } function doDragenter(control,length) { maxLength = length; value = control.value; if(maxLength){ event.returnValue = false; } } function addEvent(elm, evType, fn, useCapture) { if (elm.addEventListener) { elm.addEventListener(evType, fn, useCapture); return true; } else if (elm.attachEvent) { var r = elm.attachEvent('on' + evType, fn); return r; } else { elm['on' + evType] = fn; } } function AttacheventTextAreaBeforePaste(obj,length) { return function() { doBeforePaste(obj,length) } } function AttacheventTextAreaPaste(obj,length) { return function() { doPaste(obj,length) } } function AttacheventTextAreaKeyPress(obj,length) { return function() { doKeypress(obj,length) } } function AttacheventTextAreaDragEnter(obj,length) { return function() { doDragenter(obj,length); } } var obj = document.getElementById(controlId); addEvent(obj,'keypress',AttacheventTextAreaKeyPress(obj,length),null); addEvent(obj,'beforepaste',AttacheventTextAreaBeforePaste(obj,length),null); addEvent(obj,'paste',AttacheventTextAreaPaste(obj,length),null); addEvent(obj,'dragenter',AttacheventTextAreaDragEnter(obj,length),null); }

 

HTML代码

 

  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>  
<asp:TextBox ID="TextBoxAddress" runat="server" Width="200px" TextMode="MultiLine" Height="113px" MaxLength="10"></asp:TextBox> <script language="javascript" type="text/javascript"> SetTextAreaMaxLength('<%=TextBoxAddress.ClientID %>',10); </script>

 

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

转载于:https://www.cnblogs.com/755624068/archive/2010/01/19/1651220.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值