View Full Version : Limit Rows & chars in textarea & remove excess

03-09-2012, 04:49 PM
I have the following script which limits the number of rows and characters in a text area. I need to do this because the data gets viewed in a text ticker box and cant exceed the dimensions of the div box or will be hidden.

The script works ok, for the most part, problem being the difference in size between different characters but I have a happy medium there I think

What I would like to do with this script which is not currently handled, is also remove any excess lines or characters that get entered to the textarea exceeding the set parameters as the user types or from pasted text.
At present it just continues to count into negative numbers, which doesn't aid my need.

the script

<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;
strTemp += strChar;
strCharCounter ++;
theCharCounter.value = maxChars - strTemp.length;
theLineCounter.value = maxLines - strLineCounter;

the form that fires it, its dynamically created with php

$view = "<form action=\"#\" onsubmit=\"\$.ajax({url:'tickerTEMP.php', data: \$(this).serialize(), cache: false, type: 'post', success: function(data){\$('#showTICK').html(data);}});return false;\" name=\"myForm\">\n";
$view .= "Enter Information for Ticker $visNum<br/>\n";
$view .= "<textarea name=\"content\" cols=\"36\" rows=\"12\" wrap=\"hard\" onKeyUp=\"textCounter(myForm.content,myForm.remChars,remLines,432,12,39);\" ";
$view .= " onPaste=\"textCounter(myForm.content,myForm.remChars,remLines,432,12,39);\"";
$view .= "></textarea><br/>\n";
$view .= "<input name=\"remChars\" type=\"text\" value=\"432\" size=\"3\" maxlength=\"3\" readonly> characters left";
$view .= "<input name=\"remLines\" type=\"text\" value=\"12\" size=\"3\" maxlength=\"3\" readonly> lines left";
$view .= "<input type=\"hidden\" name=\"tickerNumber\" value=\"$number\" />\n";
$view .= "<input type=\"submit\" name=\"tickADDp\" value=\"Add Profile Ticker\" />\n";
$view .= "</form>\n";