HTML, JavaScript And CSS Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingHTML, JavaScript And CSS Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old October 11th, 2004, 12:09 PM
unicorn unicorn is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: Northern Ireland
Posts: 63 unicorn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 47 m 50 sec
Reputation Power: 5
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...>>

Reply With Quote
  #2  
Old October 11th, 2004, 03:27 PM
shamrog12's Avatar
shamrog12 shamrog12 is offline
Newton's Apple Wizard
ASP Free Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2003
Location: Los Angeles
Posts: 1,661 shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Weeks 2 Days 2 h 39 m 22 sec
Reputation Power: 34
Send a message via AIM to shamrog12
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.


Reply With Quote
  #3  
Old October 11th, 2004, 04:01 PM
unicorn unicorn is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: Northern Ireland
Posts: 63 unicorn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 47 m 50 sec
Reputation Power: 5
Quote:
Originally Posted by shamrog12
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...



yip , as u say food for thought..thats what i need...i;ll try along those lines and let u know...many thanks >>>

Reply With Quote
  #4  
Old October 11th, 2004, 04:36 PM
shamrog12's Avatar
shamrog12 shamrog12 is offline
Newton's Apple Wizard
ASP Free Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2003
Location: Los Angeles
Posts: 1,661 shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Weeks 2 Days 2 h 39 m 22 sec
Reputation Power: 34
Send a message via AIM to shamrog12
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>

Reply With Quote
  #5  
Old October 11th, 2004, 04:52 PM
unicorn unicorn is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: Northern Ireland
Posts: 63 unicorn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 47 m 50 sec
Reputation Power: 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...>>>

Reply With Quote
  #6  
Old October 11th, 2004, 08:36 PM
shamrog12's Avatar
shamrog12 shamrog12 is offline
Newton's Apple Wizard
ASP Free Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2003
Location: Los Angeles
Posts: 1,661 shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Weeks 2 Days 2 h 39 m 22 sec
Reputation Power: 34
Send a message via AIM to shamrog12
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>

Reply With Quote
  #7  
Old October 12th, 2004, 03:56 AM
unicorn unicorn is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: Northern Ireland
Posts: 63 unicorn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 47 m 50 sec
Reputation Power: 5
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..>>>

Reply With Quote
  #8  
Old October 12th, 2004, 08:20 AM
shamrog12's Avatar
shamrog12 shamrog12 is offline
Newton's Apple Wizard
ASP Free Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2003
Location: Los Angeles
Posts: 1,661 shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Weeks 2 Days 2 h 39 m 22 sec
Reputation Power: 34
Send a message via AIM to shamrog12
sorry about the misunderstanding. Good luck and write again if you get stuck.

Reply With Quote
  #9  
Old October 12th, 2004, 08:47 AM
Shadow Wizard's Avatar
Shadow Wizard Shadow Wizard is offline
Moderator From Beyond
ASP Free God 45th Plane (27000 - 27499 posts)
 
Join Date: Sep 2004
Location: Israel
Posts: 27,266 Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 3 Months 1 Week 6 Days 12 h 6 m 3 sec
Reputation Power: 1791
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.

Reply With Quote
  #10  
Old October 12th, 2004, 09:03 AM
shamrog12's Avatar
shamrog12 shamrog12 is offline
Newton's Apple Wizard
ASP Free Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2003
Location: Los Angeles
Posts: 1,661 shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Weeks 2 Days 2 h 39 m 22 sec
Reputation Power: 34
Send a message via AIM to shamrog12
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.

Reply With Quote
  #11  
Old October 12th, 2004, 09:10 AM
Shadow Wizard's Avatar
Shadow Wizard Shadow Wizard is offline
Moderator From Beyond
ASP Free God 45th Plane (27000 - 27499 posts)
 
Join Date: Sep 2004
Location: Israel
Posts: 27,266 Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 3 Months 1 Week 6 Days 12 h 6 m 3 sec
Reputation Power: 1791
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...

Reply With Quote
  #12  
Old October 13th, 2004, 07:09 AM
unicorn unicorn is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: Northern Ireland
Posts: 63 unicorn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 47 m 50 sec
Reputation Power: 5
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...>>>

Reply With Quote
  #13  
Old October 13th, 2004, 07:23 AM
Shadow Wizard's Avatar
Shadow Wizard Shadow Wizard is offline
Moderator From Beyond
ASP Free God 45th Plane (27000 - 27499 posts)
 
Join Date: Sep 2004
Location: Israel
Posts: 27,266 Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 3 Months 1 Week 6 Days 12 h 6 m 3 sec
Reputation Power: 1791
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...

Reply With Quote