View Full Version : Putting newline in form input

02-26-2003, 06:59 PM
Anyway to put in a newline or something to stop my output from creating a long unwrapped line? I want to make some function onsubmit where it forces a newline character after 10 characters. I seem to have a problem with my input form where it doesnt wrap after I enter the data and long sentences force my output box to look like a rectangle.

Here is what is happening:

Long sentence keeps going and going where it extends my output box too long.

I need it to wrap in my output such as:

Long sentence keeps going and going
where it extends my output box too long.

Here is my html form part where I need to use a form input for my entry:

<form action="mypage.html" method=post>
<TD><input name="heading" type="text" size=30></TD>

This is in a calendar script and I cant use <textarea> so I was hoping to make a function to force newline after 10 characters.

Roy Sinclair
02-26-2003, 07:59 PM
<input name="heading" type="text" size="10" maxlength="30">

Use size to control how large the input field is, use maxlength to control the maximum number of characters that the field can contain.

02-26-2003, 08:27 PM
Thanks I thought of that but need it to be able to handle sentences. Any other suggestions. I still want to try and put a newline in there and force it to break after 10 characters.

02-26-2003, 09:11 PM
This seems to work...

<script type="text/javascript">

function forceNewlines( ta, lineLen )
var r = new RegExp( "(.{" + lineLen + "})$", 'g' );
ta.value = ta.value.replace( r, "$1\n" );




<textarea rows="6" cols="40" onkeydown="forceNewlines( this, 10 )"></textarea>
<input type="reset" value="clear" />


02-26-2003, 09:37 PM
Thanks, I will give it a try. I hope I can use it with "onsubmit" instead of "onkeydown".