【JavaScript】在文本域中对输入字符及行数的限制
2010-09-25 13:43 Peter Jin 阅读(442) 评论(0) 编辑 收藏 举报<html>
<Script Language = "JavaScript">
<!--
function textCounter(theField,theCharCounter,theLineCounter,maxChars,maxLines,maxPerLine)
{
var strTemp = "";
var strLineCounter = 0;
var strCharCounter = 0;
for (var i = 0; i < theField.value.length; i++)
{
var strChar = theField.value.substring(i, i + 1);
if (strChar == '\n')
{
strTemp += strChar;
strCharCounter = 1;
strLineCounter += 1;
}
else if (strCharCounter == maxPerLine)
{
strTemp += '\n' + strChar;
strCharCounter = 1;
strLineCounter += 1;
}
else
{
strTemp += strChar;
strCharCounter ++;
}
}
theCharCounter.value = maxChars - strTemp.length;
theLineCounter.value = maxLines - strLineCounter;
}
//-->
</Script>
<form name="theForm" method="post" action="">
<textarea name="myText" cols="60" rows="10" wrap="VIRTUAL" onKeyUp="textCounter(theForm.myText,theForm.remChars,remLines,15,3,5);"></textarea>
<br>剩余<input name=remChars type=text value="15" size=3 maxlength=3 readonly>个输入字符
<br>剩余<input name=remLines type=text value="3" size=3 maxlength=3 readonly>输入行
</form>
</html>
<Script Language = "JavaScript">
<!--
function textCounter(theField,theCharCounter,theLineCounter,maxChars,maxLines,maxPerLine)
{
var strTemp = "";
var strLineCounter = 0;
var strCharCounter = 0;
for (var i = 0; i < theField.value.length; i++)
{
var strChar = theField.value.substring(i, i + 1);
if (strChar == '\n')
{
strTemp += strChar;
strCharCounter = 1;
strLineCounter += 1;
}
else if (strCharCounter == maxPerLine)
{
strTemp += '\n' + strChar;
strCharCounter = 1;
strLineCounter += 1;
}
else
{
strTemp += strChar;
strCharCounter ++;
}
}
theCharCounter.value = maxChars - strTemp.length;
theLineCounter.value = maxLines - strLineCounter;
}
//-->
</Script>
<form name="theForm" method="post" action="">
<textarea name="myText" cols="60" rows="10" wrap="VIRTUAL" onKeyUp="textCounter(theForm.myText,theForm.remChars,remLines,15,3,5);"></textarea>
<br>剩余<input name=remChars type=text value="15" size=3 maxlength=3 readonly>个输入字符
<br>剩余<input name=remLines type=text value="3" size=3 maxlength=3 readonly>输入行
</form>
</html>