Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do we limit the length of each row to 30 characters for Text area?

Posted on 2008-06-16
6
Medium Priority
?
681 Views
Last Modified: 2008-06-17
I have a textarea and I need to limit the text area to 30 characters per each row.
suppose if I try to enter more than 30 characters continuously, then the cursor should automatically go to next line at 31st character or it should leave a space, then start entering 31st character.
I have to implement this in classic ASP/JavaScript/VBScript.

any ideas please?

thanks
 
0
Comment
Question by:ipjyo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 47

Expert Comment

by:apache09
ID: 21798907
0
 

Author Comment

by:ipjyo
ID: 21799020
I think that post will handle to have a line break after so many characters when displayed.

But I dont get how do we handle it to go to a next line or enter space at 31st character while entering the input only. (but not after entering everything)
Basically I wanted the cursor to stop at 31st character and go to a new line.

thanks for your help
0
 
LVL 2

Expert Comment

by:ayus80
ID: 21799051
I think, try using javascript page. I have create sample code, try this.
<html>
<head>
<script language="javascript" type="text/javascript">
 
function limitText(limitField, limitNum) {
 
var v = window.document.myform.nextarea;
 
	if (limitField.value.length > limitNum) {
    v.onFocus = "";
	} 
}
</script>
</head>
 
<body>
<form name="myform">
<textarea name="limitedtextarea" onKeyDown="limitText(this.form.limitedtextarea,30);" 
onKeyUp="limitText(this.form.limitedtextarea,30);">
</textarea>
 
<br><br>
 
<textarea name="nextarea" onFocus="blur()">
</textarea>
 
<br>
</form>
</body>
</html>

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Accepted Solution

by:
gops1 earned 2000 total points
ID: 21799308
Try this code:


<html>
	<head>
		<title>Limit Textarea</title>
		<style type="text/css">
			body{
				font-family:verdana;
				font-size:12px;
			}
 
			textarea{
				font-family:verdana;
				font-size:10px;
				width:300px;
				height:200px
			}
 
		</style>
		<script type="text/javascript">
 
			var alert_title='Input Restriction';
 
			function limitTextarea(el,maxLines,maxChar){
				if(!el.x){
					el.x=uniqueInt();
					el.onblur=function(){clearInterval(window['int'+el.x])}
				}
				window['int'+el.x]=setInterval(function(){
					var lines=el.value.replace(/\r/g,'').split('\n'),
					i=lines.length,
					lines_removed,
					char_removed;
					if(maxLines&&i>maxLines){
						alert('You can not enter\nmore than '+maxLines+' lines');
						lines=lines.slice(0,maxLines);
						lines_removed=1
					}
					if(maxChar){
						i=lines.length;
						while(i-->0)if(lines[i].length>maxChar){
							lines[i]=lines[i].slice(0,maxChar);
							char_removed=1
						}
						if(char_removed)alert('You can not enter more\nthan '+maxChar+' characters per line')
					}
					if(char_removed||lines_removed)el.value=lines.join('\n')
				},50);
			}
 
			function uniqueInt(){
				var num,maxNum=100000;
				if(!uniqueInt.a||maxNum<=uniqueInt.a.length)uniqueInt.a=[];
				do num=Math.ceil(Math.random()*maxNum);
				while(uniqueInt.a.hasMember(num))
					uniqueInt.a[uniqueInt.a.length]=num;
				return num
			}
 
			Array.prototype.hasMember=function(testItem){
			var i=this.length;
			while(i-->0)if(testItem==this[i])return 1;
				return 0
			};
 
			function set_ie_alert(){
				window.alert=function(msg_str){
					vb_alert(msg_str)
				}
			}
 
	</script>
 
</head>
<body>
	This Text box will is going to set the limit of 6 lines and 30 chars per line<br>
	<textarea onfocus="limitTextarea(this,6,30)" wrap="off">some text</textarea>
	<br><br><br>
	This Text box will is going to set the limit of 2 lines and 10 chars per line<br>
	<textarea onfocus="limitTextarea(this,2,10)" wrap="off">some text</textarea>
</body>
</html>

Open in new window

0
 

Author Comment

by:ipjyo
ID: 21804070
I will try this and let you know

thanks for your help
0
 

Author Comment

by:ipjyo
ID: 21804304
It is simply perfect...you made my work easier...
thanks a lot and really appreciate it.

0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question