If you simply want to limit the number of words in the textarea and don't want to write too much, you can use:
o Set up the method to automatically check all textareas:
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);
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn