TextArea 控件的最大长度问题(js json) 程序员文章站 2023-12-12 19:38:40 如果只是单纯地想限制 textarea 中的字数,不想写太多的话,可用: 如果只是单纯地想限制 textarea 中的字数,不想写太多的话,可用: <textarea onkeyup="this.value = this.value.slice(0, 80)"></textarea> 或 <textarea onkeyup="this.value = this.value.substring(0, 80)"></textarea> 在 textarea 的 onkeyup 中检查字数之方法: 复制代码 代码如下: <script language="javascript" type="text/javascript"> function djcheckmaxlength(oinobj) { var imaxlen = parseint(oinobj.getattribute('maxlength')); var icurlen = oinobj.value.length; if ( oinobj.getattribute && icurlen > imaxlen ) { oinobj.value = oinobj.value.substring(0, imaxlen); } } //@ end of djcheckmaxlength() </script> <textarea maxlength="80" onkeyup="return djcheckmaxlength(this);"></textarea> o 设定好自动去检查所有的 textarea 之方法: 复制代码 代码如下: <head> <script language="javascript" type="text/javascript"> function djtextarea_addmaxlengthattr() { var ax = document.getelementsbytagname('textarea'); for ( var ii = 0; ax.length > ii; ii++ ) { if ( ax[ii].getattribute('maxlength') ) { ax[ii].onkeyup = ax[ii].onchange = djtextarea_checkmaxlength; } } } //@ end of djtextarea_addmaxlengthattr() function djtextarea_checkmaxlength() { var imaxlen = parseint(this.getattribute('maxlength')); var icurlen = this.value.length; if ( this.getattribute && icurlen > imaxlen ) { this.value = this.value.substring(0, imaxlen); } } //@ end of djtextarea_checkmaxlength() </script> </head> <body onload="djtextarea_addmaxlengthattr();"> 复制代码 代码如下: var inputs = { onintegeronly: function() { var k = system.isie ? event.keycode : event.which; if(event.ctrlkey || event.ctrlalt || event.shiftkey) { inputs.disableinput(event); return; } if ( ( k < 48 || k > 57 ) && 8 != k && 9 != k && 46 != k && 37 != k && 39 != k && 16 != k && 36 != k && 35 != k && 17 != k && 13 != k && ( k < 96 || k > 105 ) ) { inputs.disableinput(event); } return k; }, ondecimalonly: function() { var k = system.isie ? event.keycode : event.which; if(event.ctrlkey || event.ctrlalt || event.shiftkey) { inputs.disableinput(event); return; } if( ( k < 48 || k > 57 ) && 8 != k && 9 != k && 46 != k && 37 != k && 39 != k && 16 != k && 36 != k && 35 != k && 17 != k && 13 != k && 190 != k && 110 != k && ( k < 96 || k > 105 ) ) { inputs.disableinput(event); } return k; }, onasciionly: function() { var k = system.isie ? event.keycode : event.which; if ( ( k < 97 || k > 122 ) && ( k < 65 || k > 90 ) && ( k < 48 || k > 57 ) && 8 != k && 9 != k && 46 != k && 37 != k && 39 != k && 16 != k && 36 != k && 35 != k && 17 != k && 13 != k && 189 != k && 187 != k ) { inputs.disableinput(event); } return k; }, disableinput: function(event) { if(system.isie) event.returnvalue = false; else event.preventdefault(); }, addonpos: function(obj, charvalue) { //obj代表要插入字符的输入框 //value代表要插入的字符 obj.focus(); var r = document.selection.createrange(); var ctr = obj.createtextrange(); var i; var s = obj.value; //注释掉的这种方法只能用在单行的输入框input内 //对多行输入框textarea无效 //r.setendpoint("starttostart", ctr); //i = r.text.length; //取到光标位置----start---- var ivalue = "&^asdjfls2ffff325%$^&"; r.text = ivalue; i = obj.value.indexof(ivalue); r.movestart("character", -ivalue.length); r.text = ""; //取到光标位置----end---- //插入字符 obj.value = s.substr(0,i) + charvalue + s.substr(i,s.length); ctr.collapse(true); ctr.movestart("character", i + charvalue.length); ctr.select(); }, maxlength: function() { var _this = event.srcelement; var imaxlen = parseint(_this.getattribute('maxlength')); switch(event.type) { case 'beforepaste': { event.returnvalue = false; } break; case 'paste': { var itxtlen = _this.value.lenutf8(); var txtcbd = window.clipboarddata.getdata("text"); var icbdlen = txtcbd.lenutf8(); if((imaxlen - itxtlen) <= icbdlen) { var isurpluslen = imaxlen - itxtlen; var substr = txtcbd.substr(0, isurpluslen); var insertstr = ''; for(var i = 0; i < substr.length; i++) { var chr = substr.substr(i, 1); if(isurpluslen >= chr.lenutf8()) { insertstr += chr; isurpluslen -= chr.lenutf8(); } } inputs.addonpos(_this, insertstr); //_this.style.backgroundcolor = 'red'; //system.settimeout(inputs.colortips, 500, _this); event.returnvalue = false; event.srcelement.style.imemode = 'disabled'; } else { event.returnvalue = true; event.srcelement.style.imemode = 'auto'; } } break; case 'keypress': { var itxtlen = _this.value.lenutf8(); if(imaxlen <= itxtlen) { while(_this.value.lenutf8() > imaxlen) { _this.value = _this.value.substr(0, _this.value.length - 1); } inputs.disableinput(event); if('disabled' != event.srcelement.style.imemode) { event.srcelement.style.imemode = 'disabled'; } } else { if('auto' != event.srcelement.style.imemode) { event.srcelement.style.imemode = 'auto'; } } } break; case 'keyup': { var k = system.isie ? event.keycode : event.which; if (8 != k && 9 != k && 46 != k && 37 != k && 39 != k && 16 != k && 36 != k && 35 != k && 17 != k && 13 != k && 189 != k && 187 != k ) { if(_this.getattribute) { _this.fireevent('onkeypress'); } } } break; case 'keydown': { var k = system.isie ? event.keycode : event.which; if (8 != k && 9 != k && 46 != k && 37 != k && 39 != k && 16 != k && 36 != k && 35 != k && 17 != k && 13 != k && 189 != k && 187 != k ) { var itxtlen = _this.value.lenutf8(); if(imaxlen <= itxtlen) { inputs.disableinput(event); } } } break; } }, textareamaxlength: function() { var ax = document.getelementsbytagname('textarea'); for(var i = 0; i < ax.length; i++) { if(ax[i].getattribute('maxlength')) { ax[i].detachevent('onbeforepaste', inputs.maxlength); ax[i].detachevent('onpaste', inputs.maxlength); ax[i].detachevent('onkeypress', inputs.maxlength); ax[i].detachevent('onkeyup', inputs.maxlength); ax[i].detachevent('onkeydown', inputs.maxlength); ax[i].attachevent('onbeforepaste', inputs.maxlength); ax[i].attachevent('onpaste', inputs.maxlength); ax[i].attachevent('onkeypress', inputs.maxlength); ax[i].attachevent('onkeyup', inputs.maxlength); ax[i].attachevent('onkeydown', inputs.maxlength); /* var res = 'zh-cn' == window.navigator.systemlanguage ? '最大长度:' : 'maximum length for: '; if('' == ax[i].title) ax[i].title = res + ax[i].getattribute('maxlength'); */ } } }, textboxmaxlength: function() { var ax = document.getelementsbytagname('input'); for(var i = 0; i < ax.length; i++) { var maxlen = ax[i].getattribute('maxlength'); if('text' == ax[i].type && null != maxlen && '' != maxlen && undefined != maxlen && 2147483647 > parseint(maxlen) && '' == ax[i].title) { ax[i].detachevent('onbeforepaste', inputs.maxlength); ax[i].detachevent('onpaste', inputs.maxlength); ax[i].detachevent('onkeypress', inputs.maxlength); ax[i].detachevent('onkeyup', inputs.maxlength); ax[i].attachevent('onbeforepaste', inputs.maxlength); ax[i].attachevent('onpaste', inputs.maxlength); ax[i].attachevent('onkeypress', inputs.maxlength); ax[i].attachevent('onkeyup', inputs.maxlength); /* var res = 'zh-cn' == window.navigator.systemlanguage ? '最大长度:' : 'maximum length for: '; ax[i].title = res + ax[i].getattribute('maxlength'); */ } } } } tools.inputs = inputs; window.attachevent('onload', tools.inputs.textareamaxlength); window.attachevent('onload', tools.inputs.textboxmaxlength); 上一篇: 下一篇: 推荐阅读 TextArea 控件的最大长度问题(js json) TextArea 控件的最大长度问题(js json) TextArea 控件的最大长度问题(js json)_表单特效 TextArea 控件的最大长度问题(js json)_表单特效