473,414 Members | 1,705 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,414 software developers and data experts.

<textarea> function to replace Returns and Spaces

I'm working on a form to collect data in a textarea which and am trying to keep returns and spaces. I have a couple of functions that I Frankensteined together to replace returns with <br> and to replace spaces with &nbsp;. The <br> part works well enough, but I keep getting "%20" instead of "&nbsp;" for the spaces.

I understand that escape() changes " " to "%20", but I would think the ConvertSpaces function below would change the %20 to &nbsp;, but it doesn't. (FYI, I need &nbsp; instead of %20 because it appears to be the only "space" code that the resulting shopping cart page will accept for multiple sequential spaces.)

I know there's lots about this on the web, but apparently I'm too unskilled to understand anything I'm finding.

<input onclick="ConvertCarriageReturns(this.form.op31,'&l t;br&gt;')" type="image" name="add" /></form>

function ConvertCarriageReturns(textarea, strReplace)
{
textarea.value = escape(textarea.value)
for(i=0;i<textarea.value.length;i++)
{
if(textarea.value.indexOf("%0D%0A") > -1 )
{
textarea.value = textarea.value.replace("%0D%0A",strReplace)
}
}
ConvertSpaces(unescape(textarea.value),'&nbsp;')
}


function ConvertSpaces(textarea, strReplace)
{
textarea.value = escape(textarea.value)
for(i=0;i<textarea.value.length;i++)
{
if(textarea.value.indexOf(" ") > -1 )
{
textarea.value = textarea.value.replace(" ",strReplace)
}
}
textarea.value = unescape(textarea.value)
}
May 15 '07 #1
3 5608
iam_clint
1,208 Expert 1GB
i believe you have to replace " " in your serverside code... because it will escape it to %20 also i think.
May 15 '07 #2
Rrrggh. I was really hoping you were going to tell me I missed a semicolon or something easy like that. This is reaching beyond my realm of personal ability now.
May 15 '07 #3
pbmods
5,821 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1. for(i=0;i<textarea.value.length;i++)
  2. {
  3. if(textarea.value.indexOf(" ") > -1 )
  4. {
  5. textarea.value = textarea.value.replace(" ",strReplace)
  6. }
  7. }
  8. textarea.value = unescape(textarea.value)
This might not work properly, especially if, for example, two spaces are next to each other and strReplace is null.

Try this instead:

Expand|Select|Wrap|Line Numbers
  1. if(String(strReplace).indexOf(' ') > -1) {
  2.     alert('strReplace contains a space!\n"' + strReplace + '");
  3.     return false;
  4. }
  5.  
  6. textarea.value = textarea.value.replace(/ /g, strReplace);
  7.  
If you don't want to use regular expressions, you could just use the code you had, but without the for loop:
Expand|Select|Wrap|Line Numbers
  1. while(textarea.value.indexOf(' ') > -1)
  2.     textarea.value = textarea.value.replace(' ', strReplace);
  3.  
May 16 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Matt | last post by:
Simple vbscript,html page loading into access. Input pg posting to Confirm pg, then sending to access. when using; <textarea name="name" tabindex="15"> </textarea> as an input renders tab...
1
by: Augustus | last post by:
Hiya, I have a form with a <textarea></textarea> to receive user input. This input is then stored in a database and sent by fax... I need to be able to remove the carriage returns (enter...
4
by: Dennis Allen | last post by:
Hi. I hope someone here can help. I'm webmaster for a local astronomy club. Just went over our web site. Have validated every htm file on the site except:...
2
by: Jonathan Taub | last post by:
This may seem a stupid question. I've got a <textarea> element: .... <td> List of items: <textarea> 1. Apple 2. Orange 3. Box
11
by: Les Paul | last post by:
I'm trying to design an HTML page that can edit itself. In essence, it's just like a Wiki page, but my own very simple version. It's a page full of plain old HTML content, and then at the bottom,...
3
by: Jarek Mielcarek | last post by:
hi all, in xml file I have some fields which are source for <textarea> element. I'd like to transform this file using xslt and set the rows property of <textarea> depend of lines in some source...
8
by: ASP Yaboh | last post by:
I have an ArrayList of data gathered from a database. I want to create a web page from this data by creating a <table>, each cell in each row displays the appropriate data. One of those cells in...
5
by: Icarus - iD_Ten_T helper | last post by:
First of all, my apologies if this should be in a php newsgroup and not here, but I thought this the best place to start. I want to parse the text from a <textareaform element but when I pass...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.