JavaScript 限制文本框输入长度(支持字符限制)

From , 4 Years ago, written in JavaScript, viewed 53 times.
URL https://pastebin.vip/view/873be070
  1. function limit(){    
  2.     var txtNote;//文本框    
  3.     var txtLimit;//提示字数的input    
  4.     var limitCount;//限制的字数    
  5.     var isbyte;//是否使用字节长度限制(1汉字=2字符)    
  6.     var txtlength;//到达限制时,字符串的长度    
  7.     var txtByte;    
  8.     this.init=function(){    
  9.         txtNote=this.txtNote;    
  10.         txtLimit=this.txtLimit;    
  11.         limitCount=this.limitCount;    
  12.         isbyte=this.isbyte;    
  13.         txtNote.onkeydown=function(){wordsLimit()};txtNote.onkeyup=function(){wordsLimit()};    
  14.         txtLimit.value=limitCount;          
  15.     }        
  16.     function wordsLimit(){    
  17.         var noteCount=0;            
  18.         if(isbyte){noteCount=txtNote.value.replace(/[^/x00-/xff]/g,"xx").length}else{noteCount=txtNote.value.length}    
  19.         if(noteCount>limitCount){    
  20.             if(isbyte){    
  21.                 txtNote.value=txtNote.value.substring(0,txtlength+Math.floor((limitCount-txtByte)/2));    
  22.                 txtByte=txtNote.value.replace(/[^/x00-/xff]/g,"xx").length;              
  23.                 txtLimit.value=limitCount-txtByte;    
  24.             }else{    
  25.                 txtNote.value=txtNote.value.substring(0,limitCount);    
  26.                 txtLimit.value=0;    
  27.             }        
  28.         }else{    
  29.             txtLimit.value=limitCount-noteCount;    
  30.         }    
  31.         txtlength=txtNote.value.length;//记录每次输入后的长度    
  32.         txtByte=txtNote.value.replace(/[^/x00-/xff]/g,"xx").length;    
  33.     }    
  34. }    
  35.  
  36.  
  37.  
  38.  
  39. //javascript/6259

Reply to "JavaScript 限制文本框输入长度(支持字符限制)"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website