Solved

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

Posted on 2008-06-16
6
672 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 500 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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

717 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