Limit Characters and Lines
Matt Temple Dec 14, 2002
This script limits characters and lines within a textarea. You specify the characters per line, total lines and total characters. The script updates 2 counter fields (remaining characters and remaining lines). Implement an alert message in the function or use the counter fields for form validation.
Works in most browsers.
The JavaScript Source: Page Details: Limit Characters and Lines
Simply click inside the window below, use your cursor to highlight the script, and copy (type Control-c or Apple-c) the script into a new file in your text editor (such as Note Pad or Simple Text) and save (Control-s or Command-s). The script is yours!!!
<!-- TWO STEPS TO INSTALL LIMIT CHARACTERS AND LINES:
1. Copy the coding into the HEAD of your HTML document
2. Add the last code into the BODY of your HTML document -->
<!-- STEP ONE: Paste this code into the HEAD of your HTML document -->
<HEAD>
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://www.javascriptsource.com -->
<!-- Original: Matt Temple (matt@dspalum.com) -->
<!-- Web Site: http://www.dspalum.com -->
<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>
</HEAD>
<!-- STEP TWO: Copy this code into the BODY of your HTML document -->
<BODY>
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://www.javascriptsource.com -->
<!-- Original: Matt Temple (matt@dspalum.com) -->
<!-- Web Site: http://www.dspalum.com -->
<form name="theForm" method="post" action="">
<textarea name="myText" cols="100" rows="10" wrap="VIRTUAL" onKeyUp="textCounter(theForm.myText,theForm.remChars,remLines,6000,165,100);"></textarea>
<br><input name=remChars type=text value="6000" size=3 maxlength=3 readonly> characters left
<br><input name=remLines type=text value="165" size=3 maxlength=3 readonly> lines left
</form>
<p><center>
<font face="arial, helvetica" size"-2">Free JavaScripts provided<br>
by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
</center><p>
<!-- Script Size: 2.10 KB --> Did you use this script? Do you like this site? Please link to us!
Leave a Response
(0 comments)