Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Want to limit number of lines and characters in textarea

Status
Not open for further replies.

Alterego11

Programmer
May 5, 2008
2
CA
Hello,

I am trying to limit the number of lines and characters per line entered by a user in a textarea. My current script handles the number of lines but I am having trouble limiting the number of characters the user is allowed to enter on the last line. Here's the code:

<script language="JavaScript" type="text/javascript">
// this function stores the given text in a temp variable
var m_strLastValue="";
function StoreLastValue(objTextArea)
{
m_strLastValue = objTextArea.value;
}

// this function limits the number of char per line and the number of lines
var char_count = 0;
insrtTxt = function (objTextArea, iMaxLines, iMaxCharsInLine) {
//get all the text:
var strAllText=objTextArea.value;
var char_value =objTextArea.value.length;

//get lines:
var arrLines=strAllText.split("\r\n");

//get amount of lines:
var iLinesCount=(strAllText == "")?0:arrLines.length;

// 20th line: reset char counter
if (iLinesCount == iMaxLines) {
char_count = char_count + 1 ;
if (char_count > iMaxCharsInLine) {
alert("Success!");
//objTextArea.value = m_strLastValue;
char_value = 0;
}
}

//check if maximum lines count exceeded:
if (iLinesCount > iMaxLines)
{
alert("Maximum lines achieved");
objTextArea.value = m_strLastValue;
// return false;
}

return true;
}
</script>

<textarea name="verseBkgrndTxtGrp" rows=10 cols=80 wrap=hard onKeyPress="StoreLastValue(verseBkgrndTxtGrp);" onpaste="insrtTxt(verseBkgrndTxtGrp,20,60);" onKeyUp="insrtTxt(verseBkgrndTxtGrp,20,60);"></textarea>


Thanks,
I.A.

 
It doesn't look like you're controlling or computing the characters/line at all. You get the total number of characters in all lines, char_value. Then you set a counter, char_count, equal to one more than it was if (and only if) the number of lines is equal to the maximum number of lines. Since char_count was otherwise 0, it will, at most, be 1.

Somewhere, it seems to me, you need to loop through the elements of arrLines and get each one's character count (length).

_________________
Bob Rashkin
 
Are you sure you don't want to use the maxlength="1250" text area property?
or this

var arrLines=strAllText.split("\r\n");
if(arrLines.length>maxLines)
{

alert('toManyLines');
var newArray=arrLines.splice(0,maxLines);
objTextArea.value =newArray.join("\r\n");
}
else
{
for(var =0;x<arrLines.length;i++)
{
if(arrLines[x].length>maxCharsPerLine)
{
alert("too many lines on line "+x);
arrLines[x]=arrLines[x].substr(0,maxCharsPerLine);
}
}
objTextArea.value =arrLines.join("\r\n");
}

//NOTE I didn't test this
 
hexaplus:

after a few minor rectifications, your script seems to work! However, there is one problem remaining: Once I start typing, I cannot go back to a previous line and insert some additional text. The cursor is automatically moved at the last character entered in the textbox. The only way I can change previous text is to erase everything up to it and then re-write! How to handle this problem?

Here's your scriplet again - tested (ignore function StoreLastValue)!

<script language="JavaScript" type="text/javascript">
// this function stores the given text in a temp variable
var m_strLastValue="";
function StoreLastValue(objTextArea)
{
m_strLastValue = objTextArea.value;
}

// this function limits the number of char per line and the number of lines
var char_count = 0;
insrtTxt = function (objTextArea, iMaxLines, iMaxCharsInLine) {
var strAllText=objTextArea.value;
var arrLines=strAllText.split("\r\n");
if(arrLines.length > iMaxLines)
{
alert('toManyLines');
var newArray=arrLines.splice(0,iMaxLines);
objTextArea.value = newArray.join("\r\n");
}
else
{
for(var x=0;x<arrLines.length;x++)
{
if(arrLines[x].length>iMaxCharsInLine)
{
alert("too many lines on line "+x);
arrLines[x]=arrLines[x].substr(0,iMaxCharsInLine);
}
}
objTextArea.value =arrLines.join("\r\n");
}


return true;
}
</script>

<textarea name="verseBkgrndTxtGrp" rows=10 cols=80 wrap=hard onKeyPress="StoreLastValue(verseBkgrndTxtGrp);"

onpaste="insrtTxt(verseBkgrndTxtGrp,20,60);" onKeyUp="insrtTxt(verseBkgrndTxtGrp,20,60);"></textarea>

Thanks again,
I.A.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top