TextArea 控件的最大长度问题(js json)

时间:2021-05-26

如果只是单纯地想限制 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);

}
}
},

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);

}
}
}
}

Tools.Inputs = Inputs;

window.attachEvent('onload', Tools.Inputs.TextAreaMaxLength);
window.attachEvent('onload', Tools.Inputs.TextBoxMaxLength);

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章