Solved

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

Posted on 2008-06-16
6
653 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
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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
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…

770 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