为eReference(文本区域名称)键入了3行。它仅允许输入3行,每行长度为40个字符。空格也被视为一个字符。
<td>
<cong:textarea name="eReference" id="eReference" cols="40" rows="3"
styleClass="textlabelsBoldForTextBox textCapsLetter commonClass"
value="${lclBlForm.eReference}" style="text-align:left;margin-top:-55px;overflow:hidden;" onkeypress="limitText12(this)"/>
</td>
</tr>
文件名-erefer.js
function limitText12(ev){
var text = $(ev).val();
// if(text.length()== 40){
// text("\n");
// }
var arr = text.split("\n");
if(arr.length > 4) {
alert("You've exceeded the 4 line limit!");
event.preventDefault(); // prevent characters from appearing
} else {
for(var i = 0; i < arr.length; i++) {
if(arr[i].length > 40 && i < 3) {
alert("Length exceeded in line 1, 2, or 3!");
event.preventDefault(); // prevent characters from appearing
}
}
}
}
最佳答案
只需将textarea的大小增加到cols =“ 45”即可。这样错误提示将出现在同一行中。并比较大于3而不是4的数组长度。然后将只允许3行。
<script type="text/javascript">
function limitText12(ev){
var text = ev.value;
var arr = text.split("\n");
if(arr.length > 3) {
alert("You've exceeded the 4 line limit!");
event.preventDefault(); // prevent characters from appearing
}
else {
for(var i = 0; i < arr.length; i++) {
if(arr[i].length > 39) {
var j =i+1;
alert("Length exceeded in line " + j);
event.preventDefault(); // prevent characters from appearing
}
}
}
}
</script>