Limit the number of rows in a Textarea

Hello,

The problem is that I can limit the number of characters, but I have to limit the number of rows. That’s because when you insert a long word the string will be more lines than when you use short words.

I use this for TeleText. The problem is that the input of the textarea mat only contain 3 lines, each a maximum of 37 characters.
The length of characters can easily be set by usiong the font monospace or courier, but the number of rows is a lot harder.

It’s not easy to explain, but you can see it like this:

I have an textarea. The max rows is 2, the max character is 37 (or less) each row. So if you use long words, there the 2 lines can contain less information. If you use short words, the lines can contain more words.

E.G.
Long words
This is a long word:
bladibladibladibla and i
want to use 2 lines

Now I use 3 lines

Short words
This is a long word:bla dib
lad ibla dib la and i want to use 2 lines

En that with the same number of characters.

I use this for teletext, where I can place lines of 37 characters. The value in this textarea must be 2 lines, otherwise the information at the bottom doesn’t fit.

I made one code for an textarea with 2 lines:


<script language="JavaScript">
function reclameregel(veld){
st=veld.value
st=st.replace('\
',' ')
st=st.replace('  ',' ')
kap1=38

if(st.length>=kap1){
kap1=st.lastIndexOf(' ',kap1)
kap2=kap1+39
if(st.length>=kap2) kap2=st.lastIndexOf(' ',kap2)
st=st.substring(0,kap1)+'\
'+st.substring(kap1+1,kap2+1)
}

veld.value=st
}
</script>

Can someone help me?

<textarea name="comments" cols="37" rows="2"></textarea>

lol,

and when you typ more than 2 lines, you get a scrollbar…
I need a restriction at 2 lines, not a field that contains 2 lines

<html>
<head>

<script>
// Limit Lines (10-11-2005)
// by Vic Phillips http://www.vicsjavascripts.org.uk

// Customise
var MaxCharactorsForLine=10;
var MaxLines=2;

// Functional Code
var MaxTxt='^^~£^'; // any strange string

function zxcMaxLines(zxcobj){
 if (zxcobj.value.indexOf(MaxTxt)==0){
  zxcobj.value=MaxTxt;
 return
 }
 zxcsplit=zxcobj.value.split('\\r\
');
 zxccnt=zxcsplit.length-1;
 for (zxc0=0;zxc0<zxcsplit.length;zxc0++){
  zxccnt+=Math.floor(zxcsplit[zxc0].length/MaxCharactorsForLine);
 }
 document.getElementById('Test').value='Test\
'+zxccnt+' Lines';
 if (zxccnt>=MaxLines) { alert('Max'); MaxTxt=zxcobj.value; }
}

</script>

</head>
<body>
<textarea cols=10 rows=10  onkeyup="zxcMaxLines(this);" ></textarea>
<textarea cols=10 rows=10  id="Test" ></textarea>

</body>
</html>

almost,

but now you have to delete the part that you typ in line 3.

In you’re example, how faster you typ, how more characters will be displayed in line 3

I wrote this a couple weeks ago. I found you need to check onfocus to catch a paste in the textarea


<html>
<head>
<title>Limit Textarea</title>
<style type="text/css">

textarea{
width:400px;
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('\
'),
i=lines.length,
lines_removed,
char_removed;
if(maxLines&&i>maxLines){
alert('You can not enter\
more 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\
than '+maxChar+' characters per line')
}
if(char_removed||lines_removed)el.value=lines.join('\
')
},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>
<script language="vbscript" type="text/vbs">

set_ie_alert()

Function vb_alert(msg_str)
MsgBox msg_str,vbOKOnly+vbInformation+vbApplicationModal,alert_title
End Function

</script>
</head>
<body>
<textarea onfocus="limitTextarea(this,6,40)" wrap="off">some text</textarea>
<textarea onfocus="limitTextarea(this,2,10)" wrap="off">some text</textarea>
</body>
</html>

tnx but I already made an other one:


:
<script language="JavaScript">

function testje(veld){

st=veld.value.split('\
')
st=st.join(' ')
st=st.split('\\r')
st=st.join(' ')
st=st.split('  ')
st=st.join(' ')

col=veld.cols
lengte =col-1


vorige=0
regels=new Array()
r=0

while(lengte<st.length&&r<veld.rows){
lengte=st.lastIndexOf(' ',lengte)
regels[r]=st.substring(vorige,lengte)
r++
vorige=lengte+1
lengte+=col
}
if(r<veld.rows)regels[r]=st.substring(vorige,st.length)
else veld.blur()

st=regels.join('\
')
}

function ververs(veld) {
testje(veld)
if(r>=veld.rows) alert('knipperdeknip')
veld.value =st
}
</script>
<form name="test">
<textarea name="veldje" onkeyup="testje(this);" onchange="ververs(this)" rows=5 cols=38 style="overflow:hidden;"></textarea> </form>


is it possible not to prompt an alert?? and just stop writting???