Solved

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

Posted on 2008-06-16
6
647 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now