473,324 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

how to force carriage return in textarea

i need to force a carriage return with a textarea field at X number of
characters. anybody know how to do this?

tks
Jul 23 '05 #1
8 46100
Steven wrote:
i need to force a carriage return with a textarea field at X number of
characters. anybody know how to do this?


onchange, read its value, insert the returns, and move on.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #2

"Randy Webb" <Hi************@aol.com> wrote in message
news:qZ********************@comcast.com...
Steven wrote:
i need to force a carriage return with a textarea field at X number of
characters. anybody know how to do this?
onchange, read its value, insert the returns, and move on.

whta's the carriage return?

sTxtBoxValue = sTxtBoxValue + "\r"?


--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #3
Steven wrote:

whta's the carriage return?

sTxtBoxValue = sTxtBoxValue + "\r"?

It depends where you mean to display the text. Normally you don't need
to insert carriage returns, but if it's for display in HTML page, and
you need to force a break.
sTxtBoxValue = sTxtBoxValue + "<BR>"?
But where to place the break can become problematic.

You may need to transform the text into an array.

arr=text.split(/\s+/)
Then count the characters in each array entry, and set up some kind of
"while" loop.
Mick
Mick

Jul 23 '05 #4

"Mick White" <mw***********@rochester.rr.com> wrote in message
news:sU****************@twister.nyroc.rr.com...
Steven wrote:

whta's the carriage return?

sTxtBoxValue = sTxtBoxValue + "\r"?
It depends where you mean to display the text. Normally you don't need
to insert carriage returns, but if it's for display in HTML page, and
you need to force a break.
sTxtBoxValue = sTxtBoxValue + "<BR>"?


<br> won't work inside a textarea. i need to start a new line withing
textarea, just as though the user has hit the enter key. in vb, i could use
a 'sendkeys' function to mimic keyboard behaviour. i need to do the same
thing here. this doesn't work:

<script language="javascript">
function forceReturn(iMaxLength, sValue){
if (sValue.length > iMaxLength){
sValue = sValue + "\r";
}
}
</script>

<textarea name="txt" onKeyUp="forceReturn('5', this.value);" rows="5"
cols="10"></textarea>


But where to place the break can become problematic.

You may need to transform the text into an array.

arr=text.split(/\s+/)
Then count the characters in each array entry, and set up some kind of
"while" loop.
Mick
Mick

Jul 23 '05 #5
Steven wrote:
[snip]
<script language="javascript">
function forceReturn(iMaxLength, sValue){
if (sValue.length > iMaxLength){
sValue = sValue + "\r";
}
}
</script>

<textarea name="txt" onKeyUp="forceReturn('5', this.value);" rows="5"
cols="10"></textarea>


<script type="text/javascript">
function forceReturn(iMaxLength,sValue){
if (sValue.value.length > iMaxLength){
sValue.value += "\r";
}
}
</script>

<textarea name="txt" onKeyUp="forceReturn('5', this);" rows="5"
cols="10"></textarea>

You can't change the value using :
sValue = sValue + "\r";
(sValue is a reference not an object). Better to pass the textfield
object to your function.

Mick
Jul 23 '05 #6
Steven wrote:
i need to force a carriage return with a textarea field at X number of
characters. anybody know how to do this?


You can't reliably do this, though you may be able to get it to work in
a particular browser, but certainly not all.

One of the biggest problems is that you have no idea where the
insertion point is. You can't simply add a return after 5 keystrokes,
or to the end of the string if it's more than 5 characters long.

Say you add an onkeydown event the checks the number of characters, and
when the user types a 5th character, you add a return to the end of the
string to add a 6th character.

But the cursor is still at the 5th, before your return, so any further
input will go before your return - you can't move the insertion point
programmatically.

Another is if the user puts the insertion point somewhere else in the
string and starts typing - say changing the first 5 character phrase.
you now must get rid of all your returns and put them back in the right
place - each time a key is pressed.

There are many more scenarios where this will fail, as no doubt you
have discovered.

Rob.
Jul 23 '05 #7
Mick White wrote:

<script type="text/javascript">
function forceReturn(iMaxLength,sValue){
if (sValue.value.length > iMaxLength){
sValue.value += "\r";
}
}
</script>

The above script is nonsense, sorry. Notwithstanding Rob's caveats,
the following is an improvement (It will create a column of text 5
characters wide)

<script type="text/javascript">
function forceReturn(iMaxLength,sValue){
if (sValue.value.length % iMaxLength==5){
sValue.value += "\r";
}
}
</script>
Mick
Jul 23 '05 #8
Mick White wrote:
Mick White wrote:

<script type="text/javascript">
function forceReturn(iMaxLength,sValue){
if (sValue.value.length > iMaxLength){
sValue.value += "\r";
}
}
</script>


The above script is nonsense, sorry. Notwithstanding Rob's caveats, the
following is an improvement (It will create a column of text 5
characters wide)

<script type="text/javascript">
function forceReturn(iMaxLength,sValue){
if (sValue.value.length % iMaxLength==5){
sValue.value += "\r";
}
}
</script>


I have a script somewhere that does what the OP Wants. But it works
onChange. These are the basic steps:

1) Read the value.
2) Find the nth character, where n is the desired width in characters.
3) Is the nth character a space?
If not a space: find the lastindexOf a space.Grab the substrings.
If a space:
Grab a substring from 0 to n-1, grab the substring from n to string length.
Save the first substring in an array.
Repeat with the remaining string.

Now, put your array back together with a new line character with join().
Put the string back into the textarea.

It will only work with a fixed width font.

Ahh, here it is:
http://members.aol.com/_ht_a/hikksno...ypingText.html

Its primitive at best (could be refined). But it uses a DIV tag and BR/P
tags instead of \n\r but the insertBR function could easily be modified
to work with newlines.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Canes_Rock | last post by:
The information posted at: ...
2
by: Andrew Chanter | last post by:
I have a VBA function that returns a string including "vbcr" (VB Carriage Return) to seperate a list into multiple rows, eg Item1 & vbcr & Item2 & vbcr & Item3 This works as planned in the...
2
by: eagleofjade | last post by:
I am trying to import data from a Word document into an Access table with VBA. The Word document is a form which has various fields. One of the fields is a field for notes. In some cases, this...
1
by: VMI | last post by:
If I want to separate a string in a web textbox so that it takes several lines, how can do it? In a Windows textbox, I only needed to add the "\r\n" to the string for the carriage return....
3
by: Dinsdale | last post by:
I have an xml file that is read into an object using serialization. One of the objects has a string field called delimeter that I want to contain a carriage return. Instead of trying to include the...
0
by: J.Marsch | last post by:
I am having a problem in which ASP.Net web services are corrupting my data. I know that my problem is related to the standard way of encoding carriage return linefeeds, so I need to figure out how...
1
by: Blue | last post by:
This JS limits the input characters into the form. How do I modify it so that it also allows CARRIAGE RETURN and BACKSPACE (for making text correction)? Due to the template engine I am using, I...
11
by: evenlater | last post by:
My db allows the user to send email via CDO. The body of the email is determined in code. I have built an email form with To, CC and Subject lines and a large text box for the body of the message...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.