您的位置:首页 > Web前端 > HTML

限制TextArea文本长度

2008-12-25 15:32 253 查看
maxlength.htc  
  ------------------  
  <PUBLIC:COMPONENT   id="bhvMaxlength"   urn="maf:Maxlength">  
  <PUBLIC:PROPERTY   name="maxLength"   />  
  <PUBLIC:ATTACH   event="onkeypress"   handler="doKeypress"   />  
  <PUBLIC:ATTACH   event="onbeforepaste"   handler="doBeforePaste"   />  
  <PUBLIC:ATTACH   event="onpaste"   handler="doPaste"   />  
   
  <SCRIPT   language="JScript">  
  //   Keep   user   from   entering   more   than   maxLength   characters  
  function   doKeypress(){  
  if(!isNaN(maxLength)){  
  maxLength   =   parseInt(maxLength);  
  var   oTR   =   element.document.selection.createRange();  
  //   Allow   user   to   type   character   if   at   least   one   character   is   selected  
  if(oTR.text.length   >=   1)  
  event.returnValue   =   true;  
  else   if(value.length   >   maxLength-1)  
  event.returnValue   =   false;  
  }  
  }  
  //   Cancel   default   behavior  
  function   doBeforePaste(){  
  if(!isNaN(maxLength))  
  event.returnValue   =   false;  
  }  
  //   Cancel   default   behavior   and   create   a   new   paste   routine  
  function   doPaste(){  
  if(!isNaN(maxLength)){  
  event.returnValue   =   false;  
  maxLength   =   parseInt(maxLength);  
  var   oTR   =   element.document.selection.createRange();  
  var   iInsertLength   =   maxLength   -   value.length   +   oTR.text.length;  
  var   sData   =   window.clipboardData.getData("Text").substr(0,iInsertLength);  
  oTR.text   =   sData;  
  }  
  }  
  </SCRIPT>  
   
  </PUBLIC:COMPONENT>  
  -----------  
  test.htm  
  -----------  
  <html>  
  <head>  
  </head>  
  <body>  
  <form   method="POST">  
  <p><input   type="text"   size="30"   maxlength="50"   name="T1">  
  <textarea   name="S1"   rows="4"   cols="30"   maxlength="50"   style="behavior:url(maxlength.htc)"></textarea>  
  </form>  
  </body>  
  </html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息