Alterego11
Programmer
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.
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.