|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
limit textarea
I know from searching the net that u can limit a textarea for a certain number of characters or even words, but I would like to limit mine to a certan number of lines with say 60 characters per line. When u keep typing the lines wrap but these couple of lines are still counted as one line when using any code i've found so far, and the textarea still scrolls down the page until the certain number of character limit is reached. Each RETURN pressed when filling out the textarea is counted as two chars, and the area scrolls down until the chars limit.
How can i make it stop at a particular number of lines no matter how they are filled out, even its only a few chars with the RETURN key pressed a whole lot of times...I think I need to capture each RETURN press but how do I capture a line wrap?....any help out there...>> |
|
#2
|
||||
|
||||
|
maybe put an onchange() on the textarea that searches for instances of \n? I haven't tried to get that to work so it might be a terrible idea. That's just brain food for now...
__________________
If you found a post of mine helpful, please click on the on my post to add to my reputation.
|
|
#3
|
|||
|
|||
|
Quote:
yip , as u say food for thought..thats what i need...i;ll try along those lines and let u know...many thanks >>> |
|
#4
|
||||
|
||||
|
Ok i started to play with my idea and I have something that does it but I don't know if it does it in the way you want since it's an onchange rather than onkeydown etc. onkeydown doesn't allow the user to make modifications without throwing alerts. Here you go though. Let me know in what ways this doesn't accomplish your goal:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
function detectWrap(length,value) {
var linesAllowed = 6; // number of line breaks allowed
var i; // int counter for for loop
var lineBreakCount = 0; // counter for line breaks
for (i=0; i<=length; i++) {
var myRegExp = /\n/
// test to see if the char being tested is a return
if (myRegExp.test(value.charAt(i))) {
// char is a line break so increment var
lineBreakCount += 1;
//alert("You have a linebreak between: " + value.charAt(i) + " and " + value.charAt(i+1));
}
// one break is always detected at the beginning so increment by 1
var lineBreaksDetected = lineBreakCount + 1
// if number of line breaks is too large
if ( lineBreaksDetected > linesAllowed) {
alert("too many lines. You have over " + linesAllowed + " lines and are only allowed " + linesAllowed);
break
}
}
}
</script>
</head>
<body>
<form id="wrapper">
<textarea cols="50" rows="8" onchange="detectWrap(this.value.length,this.value);"></textarea>
</form>
</body>
</html>
|
|
#5
|
|||
|
|||
|
limit textarea
Yes nearly there.....it seems to be OK, works fine except, i need to refresh the page each time after all the user input and then it tells me that i have too many lines in the alert, if i'm over six lines...the onchange dosen't seem to be firing..I will look at it closer and play around....have to go to bed now....back hopefully tomorrow...again many thanks for ur help so far....cul...>>>
|
|
#6
|
||||
|
||||
|
ok, this one is easily-expandable for other form fields and I also think that this should do what you want. It validates the textarea when you submit the form. Additional functions could be added for any/all other form elements too so you can validate the whole thing. Here's the textarea by itself:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
function validatePage(form) {
return (
detectWrap(form.elements["myTextArea"])
)
}
function detectWrap(formField) {
var length = formField.value.length;
var value = formField.value;
var linesAllowed = 6; // number of line breaks allowed
var i; // int counter for for loop
var lineBreakCount = 0; // counter for line breaks
for (i=0; i<=length; i++) {
var myRegExp = /\n/
// test to see if the char being tested is a return
if (myRegExp.test(value.charAt(i))) {
// char is a line break so increment var
lineBreakCount += 1;
}
// one break is always detected at the beginning so increment by 1
var lineBreaksDetected = lineBreakCount + 1
// if number of line breaks is too large
if ( lineBreaksDetected > linesAllowed) {
alert("too many lines. You have over " + linesAllowed + " lines and are only allowed " + linesAllowed);
return false;
}
}
// if the loop didn't return a false value, return true
return true;
}
</script>
</head>
<body>
<form>
<p>
<textarea cols="50" id="myTextArea" rows="8" onchange="detectWrap(this);"></textarea>
</p>
<p><input value="Submit Form" type="submit" id="submit" onclick="if (!validatePage(this.form)) return false;"></p>
</form>
</body>
</html>
|
|
#7
|
|||
|
|||
|
Limit Textarea
OK Shamrog12, wires starting to get crossed.....when i was saying that i needed to refresh the page for ur last design to work, that was a negitive thing...i would like (if it is possible) to be able to validate the area as the user fills it out and limit him/her to the (lets say) six lines, not have the user submit the form then tell them its too many lines....now I will try ur last design and see it work, bur havent much time at the moment.(Have visitors from Austrialia this week, so am just snatching the odd time to log on to this forum, and trying ur ideas, will be back to normal next week) so again thanks for ur time and ideas, will get back when i've tried ur latest..bye for now..>>>
|
|
#8
|
||||
|
||||
|
sorry about the misunderstanding. Good luck and write again if you get stuck.
|
|
#9
|
||||
|
||||
|
hi there!!
you can also use this code if you want: Code:
<script>
var m_strLastValue="";
function StoreLastValue(objTextArea)
{
m_strLastValue = objTextArea.value;
}
function ValidateTextArea(objTextArea, iMaxLines, iMaxCharsInLine)
{
//get all the text:
var strAllText=objTextArea.value;
//get lines:
var arrLines=strAllText.split("\r\n");
//get amount of lines:
var iLinesCount=(strAllText == "")?0:arrLines.length;
//check if maximum lines count exceeded:
if (iLinesCount > iMaxLines)
{
alert("sorry, maximum lines achieved");
objTextArea.value = m_strLastValue;
return false;
}
//check if maximum characters in line has been exceeded:
for (var i=0; i<arrLines.length; i++)
{
if (arrLines[i].length > iMaxCharsInLine)
{
alert("sorry, maximum characters in line number "+(i+1)+" achieved");
objTextArea.value = m_strLastValue;
return false;
}
}
return true;
}
</script>
<textarea onkeypress="StoreLastValue(this);" onkeyup="ValidateTextArea(this, 10, 60);"></textarea>
the above code will prevent user from typing more than 10 lines, or more than 60 characters in one line... in such case, alert message appears and the previous text is written instead. |
|
#10
|
||||
|
||||
|
there are some major issues with onkeypress which is why it's never really used. If you get the error message, clicking in the box or hitting delete to remove any offending characters will bring up the alert again so you're essentially stuck. This may not be true for IE but it is true for other browsers... which we should all be using since IE is such a huge security risk now :-/ That's why I omitted onkeypress for the first example I sent, otherwise that would do the trick.
|
|
#11
|
||||
|
||||
|
well, I use it only for storing the text in memory buffer... I've checked the code now in Mozilla Firefox and there is only one change needed:
Code:
var arrLines=strAllText.split(/\n/); as Firefox has different characters for NewLine. apart of this, it works as expected... ![]() |
|
#12
|
|||
|
|||
|
limit textarea
Thanks guys for ur latest, visitors here keeping me from trying them out yet..Hi Shadow.....thanks for ur ideas...get back to u soon...>>>
|
|
#13
|
||||
|
||||
|
no problem.. check the code I've posted I believe it's what you're after.
nice place here, I see you Joined when all the Admins joined (probably this forum is one year old only?) but you have so few posts... |