Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

Ignore spaces in textarea parsing.

Hello,

I recently posted a question on how to 'pair up' all my textarea fields using javascript code

This is the javascript someone posted that helped me:

<script type="text/javascript">
function addLineBreaksInTextArea()
{
       var txtAreaObjs = document.getElementsByTagName("textarea");

       for(var i=0;i<txtAreaObjs.length;i++){
               txtAreaObjs[i].value=txtAreaObjs[i].defaultValue; // reset the texareas to default values
               parsestring3 = txtAreaObjs[i].value.split(/,/); //Returns the array
       txtAreaObjs[i].value = parsestring3[0] + "=" + parsestring3[1] + "\n";
           txtAreaObjs[i].value = txtAreaObjs[i].value + parsestring3[2] + "=" + parsestring3[3] + "\n";
               txtAreaObjs[i].value = txtAreaObjs[i].value + parsestring3[4] + "=" + parsestring3[5] + "\n";
       }
}

</script>



This is how the text areas look like:


<textarea readOnly class="cart" wrap="physical" name="0:freeopt" rows="4" cols="40" ID="Textarea1">Product,Widget,Type,Bucket,Color,Blue</textarea>

<textarea readOnly class="cart" wrap="physical" name="1:freeopt" rows="4" cols="40" ID="Textarea2">Product,Shell,Type,Wood,Color,light blue</textarea>


This is how I get them to look now:

Product = Widget
Type = Bucket
Color = Blue

however, if there's a space in the text area values it doesn't work.  For example, in the second textarea I have a color value as  "light blue."  If I use this, the whole parsing goes wrong.  

Does anyone knows how to ignore the spaces between the textarea values so it doesn't parse wrong and I still get 3 pair of values even if there are spaces in them?  The separator should always be the commas and nothing else.

This is what I need:

Product = Widget
Type = Bucket
Color = Blue

and

Product  = Shell
Type = Wood
Color = light blue


Thanks!
0
TheUndecider
Asked:
TheUndecider
  • 2
  • 2
1 Solution
 
kadabaCommented:
I do get

Product=Widget
Type=Bucket
Color=Blue

and

Product=Shell
Type=Wood
Color=light blue

with the above script.

parsestring3 = txtAreaObjs[i].value.split(/,/); this is the place where the values are being split by ",".

I am afraid I could not get you completely
0
 
TheUndeciderAuthor Commented:
Sorry Kadaba,

My mistake.  I entered the wrong code here.  To summarize what I'm trying to do here,  for each textarea I have I'm supposed to parse them as the 3 pair values using a the space equal sign space " = ".  However, once I do so, I am not supposed to touch them.  

Here's the function I'm trying to make it work.

<script type="text/javascript">
function addLineBreaksInTextArea()
{
        var txtAreaObjs = document.getElementsByTagName("textarea");
        var regexSign = /[ = ]/g;
       
       
       for(var i=0;i<txtAreaObjs.length;i++)      
   {    
       
        if(regexSign.test(txtAreaObjs[i].value))  
       
          {  
          //nothing to do
          }
        else
      
       {      
       parsestring3 = txtAreaObjs[i].value.split(/,/); //Returns the array
       txtAreaObjs[i].value = parsestring3[0] + " = " + parsestring3[1] + "\n";
       txtAreaObjs[i].value = txtAreaObjs[i].value + parsestring3[2] + " = " + parsestring3[3] + "\n";
       txtAreaObjs[i].value = txtAreaObjs[i].value + parsestring3[4] + " = " + parsestring3[5] + "\n";
       }
       
    }
}
</script>
0
 
kadabaCommented:
The regex :

regexSign = /[=]/g;
0
 
TheUndeciderAuthor Commented:
Thank you!!!!!  
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now