<textarea> function to replace Returns and Spaces

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • FunkHouse9
    New Member
    • Apr 2007
    • 11

    <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="Conver tCarriageReturn s(this.form.op3 1,'&lt;br&gt;') " type="image" name="add" /></form>

    function ConvertCarriage Returns(textare a, strReplace)
    {
    textarea.value = escape(textarea .value)
    for(i=0;i<texta rea.value.lengt h;i++)
    {
    if(textarea.val ue.indexOf("%0D %0A") > -1 )
    {
    textarea.value = textarea.value. replace("%0D%0A ",strReplac e)
    }
    }
    ConvertSpaces(u nescape(textare a.value),'&nbsp ;')
    }


    function ConvertSpaces(t extarea, strReplace)
    {
    textarea.value = escape(textarea .value)
    for(i=0;i<texta rea.value.lengt h;i++)
    {
    if(textarea.val ue.indexOf(" ") > -1 )
    {
    textarea.value = textarea.value. replace(" ",strReplac e)
    }
    }
    textarea.value = unescape(textar ea.value)
    }
  • iam_clint
    Recognized Expert Top Contributor
    • Jul 2006
    • 1207

    #2
    i believe you have to replace " " in your serverside code... because it will escape it to %20 also i think.

    Comment

    • FunkHouse9
      New Member
      • Apr 2007
      • 11

      #3
      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.

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Originally posted by FunkHouse9
        [CODE=javascript]for(i=0;i<texta rea.value.lengt h;i++)
        {
        if(textarea.val ue.indexOf(" ") > -1 )
        {
        textarea.value = textarea.value. replace(" ",strReplac e)
        }
        }
        textarea.value = unescape(textar ea.value)[/code]
        This might not work properly, especially if, for example, two spaces are next to each other and strReplace is null.

        Try this instead:

        [code=javascript]
        if(String(strRe place).indexOf( ' ') > -1) {
        alert('strRepla ce contains a space!\n"' + strReplace + '");
        return false;
        }

        textarea.value = textarea.value. replace(/ /g, strReplace);
        [/code]

        If you don't want to use regular expressions, you could just use the code you had, but without the for loop:
        [code=javascript]
        while(textarea. value.indexOf(' ') > -1)
        textarea.value = textarea.value. replace(' ', strReplace);
        [/code]

        Comment

        Working...