Replace commas in values in an array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adrienne Boswell

    Replace commas in values in an array

    Although this is a client side issue, I am also posting to asp.general
    in case there is someway to do this only server side (which I would
    prefer).

    Here's my form:

    <form method="post" action="<%=requ est.servervaria bles("Script_na me")
    %>"
    <% for i - 0 to 4%>
    Header: <input type="text" name="header" id="header<%=i% >"
    onblur="functio n(this)"><br>
    Description: <input type="text" name="descripti on" id="description <%=i
    %>" onblur="functio n(this)">
    <% next%>
    <input type="submit" value="Submit">
    </form>

    When the data gets returned it's something like:
    header: "socks, shoes, pizza, cats, dogs"
    description: "good for feet, wear with socks, cheese only, have lots
    of hair, bark a lot"

    Problem comes when the description or header have a comma. I need a
    little javascript that will replace the comma with a semicolon client
    side before it gets to
    the server, preferably as they leave the field.

    Any help with this would be much appreciated.

    --
    Adrienne Boswell at work
    Administrator nextBlock.com

    Please respond to the group so others can share

  • Evertjan.

    #2
    Re: Replace commas in values in an array

    Adrienne Boswell wrote on 06 apr 2007 in
    microsoft.publi c.inetserver.as p.general:
    Although this is a client side issue, I am also posting to asp.general
    in case there is someway to do this only server side (which I would
    prefer).
    [...]
    Problem comes when the description or header have a comma. I need a
    little javascript that will replace the comma with a semicolon client
    side before it gets to the server, preferably as they leave the field.
    I doubt you can do anything serverside before it gets to the server,
    Adrienne.

    Clientside it is simple, just do a regex global replace.

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)

    Comment

    • Dave Anderson

      #3
      Re: Replace commas in values in an array

      Adrienne Boswell wrote:
      Problem comes when the description or header have a comma. I need
      a little javascript that will replace the comma with a semicolon
      client side before it gets to the server, preferably as they leave
      the field.
      Assuming from your example that you want the expression for your onblur
      handler, here is one way:

      onblur="this.va lue=this.value. split(',').join (';')"

      Another:

      onblur="this.va lue=this.value. replace(/,/g,';')"



      --
      Dave Anderson

      Unsolicited commercial email will be read at a cost of $500 per message. Use
      of this email address implies consent to these terms.


      Comment

      • Adrienne Boswell

        #4
        Re: Replace commas in values in an array


        Dave Anderson wote:
        Adrienne Boswell wrote:
        Problem comes when the description or header have a comma. I need
        a little javascript that will replace the comma with a semicolon
        client side before it gets to the server, preferably as they leave
        the field.
        >
        Assuming from your example that you want the expression for your onblur
        handler, here is one way:
        >
        onblur="this.va lue=this.value. split(',').join (';')"
        >
        Another:
        >
        onblur="this.va lue=this.value. replace(/,/g,';')"
        >
        >
        >
        Dave, you're an officer, a genteman and a fine judge of women! Thanks
        so much - works perfectly!

        --
        Adrienne Boswell at work
        Administrator nextBlock.com

        Please respond to the group so others can share

        Comment

        • RobG

          #5
          Re: Replace commas in values in an array

          On Apr 6, 8:02 am, "Adrienne Boswell" <arb...@yahoo.c omwrote:
          Although this is a client side issue,
          It is a server issue.

          I am also posting to asp.general
          in case there is someway to do this only server side (which I would
          prefer).
          >
          Here's my form:
          >
          <form method="post" action="<%=requ est.servervaria bles("Script_na me")
          %>"
          <% for i - 0 to 4%>
          Header: <input type="text" name="header" id="header<%=i% >"
          onblur="functio n(this)"><br>
          Description: <input type="text" name="descripti on" id="description <%=i
          %>" onblur="functio n(this)">
          <% next%>
          <input type="submit" value="Submit">
          </form>
          Don't post server code to a group concerned with client scripting,
          post whatever it is that the client gets. However you generate that
          is up to you.

          When the data gets returned it's something like:
          header: "socks, shoes, pizza, cats, dogs"
          description: "good for feet, wear with socks, cheese only, have lots
          of hair, bark a lot"
          Tell your users not to use commas and use normal validation
          techniques.

          Problem comes when the description or header have a comma. I need a
          little javascript that will replace the comma with a semicolon client
          side before it gets to
          the server, preferably as they leave the field.
          Client scripting is unreliable, deal with it at the server.

          For luck, you can use something like:

          <input onblur="this.va lue=this.value. replace(/,/g,';');" ... >


          though you are better to do the replace onsubmit when you do the rest
          of your client-side validation, users will likely get confused seeing
          their commas turn into semi-colons after they leave the field. And if
          scripting is disabled, not available or fails, you will still get
          commas in the data.


          --
          Rob

          Comment

          • Dave Anderson

            #6
            Re: Replace commas in values in an array

            "Adrienne Boswell" wrote:
            Dave, you're an officer, a genteman and a fine judge of women!
            Thanks so much - works perfectly!
            I am glad to hear it. However...

            ....I feel I should echo RobG here in warning that security (and this
            includes data integrity) really does belong on the server. So, if you are
            inclined to agree, perhaps you would consider using this approach over on
            the server side.

            JScript ASP example:
            (Request.Form(" description").I tem || "").replace (/,/g,";")


            VBScript candidate:
            Join(Split(Requ est.Form("descr iption") & "",","),";" )



            --
            Dave Anderson

            Unsolicited commercial email will be read at a cost of $500 per message. Use
            of this email address implies consent to these terms.

            Comment

            • brunascle.maps@gmail.com

              #7
              Re: Replace commas in values in an array

              On Apr 5, 6:02 pm, "Adrienne Boswell" <arb...@yahoo.c omwrote:
              Although this is a client side issue, I am also posting to asp.general
              in case there is someway to do this only server side (which I would
              prefer).
              >
              Here's my form:
              >
              <form method="post" action="<%=requ est.servervaria bles("Script_na me")
              %>"
              <% for i - 0 to 4%>
              Header: <input type="text" name="header" id="header<%=i% >"
              onblur="functio n(this)"><br>
              Description: <input type="text" name="descripti on" id="description <%=i
              %>" onblur="functio n(this)">
              <% next%>
              <input type="submit" value="Submit">
              </form>
              >
              When the data gets returned it's something like:
              header: "socks, shoes, pizza, cats, dogs"
              description: "good for feet, wear with socks, cheese only, have lots
              of hair, bark a lot"
              >
              Problem comes when the description or header have a comma. I need a
              little javascript that will replace the comma with a semicolon client
              side before it gets to
              the server, preferably as they leave the field.
              >
              Any help with this would be much appreciated.
              >
              --
              Adrienne Boswell at work
              Administrator nextBlock.com

              Please respond to the group so others can share
              you could do it in javascript using regular expression. i believe
              something like this would work:

              var someStringVaria ble = "asdfasd asdf asdfa, asdfasdf asdksdas,
              asdfas";
              someStringVaria ble = someStringVaria ble.replace(/,/, ";");

              i'd probably go about it a different way, though. i'd give each
              <inputtag a unique name, so you dont have to worry about the values
              being squashing into one value.

              Comment

              • Dave Anderson

                #8
                Re: Replace commas in values in an array

                brunascle.maps@ gmail.com wrote:
                you could do it in javascript using regular expression. i believe
                something like this would work:
                >
                var someStringVaria ble = "asdfasd asdf asdfa, asdfasdf asdksdas,
                asdfas";
                someStringVaria ble = someStringVaria ble.replace(/,/, ";");
                Don't just believe it, try it. Then you will notice that you need a global
                flag.



                --
                Dave Anderson

                Unsolicited commercial email will be read at a cost of $500 per message. Use
                of this email address implies consent to these terms.


                Comment

                • Adrienne Boswell

                  #9
                  Re: Replace commas in values in an array

                  Gazing into my crystal ball I observed "Dave Anderson"
                  <NYRUMTPELVWH@s pammotel.comwri ting in
                  news:131ci31sb1 olu61@corp.supe rnews.com:
                  "Adrienne Boswell" wrote:
                  >Dave, you're an officer, a genteman and a fine judge of women!
                  >Thanks so much - works perfectly!
                  >
                  I am glad to hear it. However...
                  >
                  ...I feel I should echo RobG here in warning that security (and this
                  includes data integrity) really does belong on the server. So, if you
                  are inclined to agree, perhaps you would consider using this approach
                  over on the server side.
                  >
                  JScript ASP example:
                  (Request.Form(" description").I tem || "").replace (/,/g,";")
                  >
                  >
                  VBScript candidate:
                  Join(Split(Requ est.Form("descr iption") & "",","),";" )
                  >
                  >
                  >
                  I don't think that's going to work, here's why:

                  Let's say that request.form("n umbers") = "1, 2, 3, 4" and request.form
                  ("descriptio n") = "eggs, bacon, milk, butter"


                  numbersarr = split(request.f orm("numbers"))
                  descarr = split(request.f orm("descriptio n"),",")
                  for i = 0 to ubound(numbersa rr)
                  response.write numbersarr(i) & "=" & descarr(i)
                  next

                  That works if the arrays are both the same size. If request.form
                  ("descriptio n") = "eggs, milk, butter, sugar, flour" then you've got a
                  problem.

                  The form I am making has 10 rows of 5 items each, so that's why I'm doing
                  it client side.

                  --
                  Adrienne Boswell at Home
                  Arbpen Web Site Design Services
                  Arbpen Consulting will help you harness valuable insights and translate them into tangible results by merging data and strategy.

                  Please respond to the group so others can share

                  Comment

                  • Evertjan.

                    #10
                    Re: Replace commas in values in an array

                    Adrienne Boswell wrote on 06 apr 2007 in
                    microsoft.publi c.inetserver.as p.general:

                    I don't think that's going to work, here's why:
                    >
                    Let's say that request.form("n umbers") = "1, 2, 3, 4" and request.form
                    ("descriptio n") = "eggs, bacon, milk, butter"
                    >
                    >
                    numbersarr = split(request.f orm("numbers"))
                    numbersarr = split(request.f orm("numbers"), ",")
                    descarr = split(request.f orm("descriptio n"),",")
                    if ubound(numbersa rr)>=ubound(des carr) then
                    max = ubound(descarr)
                    else
                    max = ubound(numbersa rr)
                    end if
                    for i = 0 to max - 1
                    ....
                    for i = 0 to ubound(numbersa rr)
                    response.write numbersarr(i) & "=" & descarr(i)
                    next
                    >
                    That works if the arrays are both the same size. If request.form
                    ("descriptio n") = "eggs, milk, butter, sugar, flour" then you've got a
                    problem.
                    See serverside solution above.
                    The form I am making has 10 rows of 5 items each, so that's why I'm
                    doing it client side.
                    ??

                    --
                    Evertjan.
                    The Netherlands.
                    (Please change the x'es to dots in my emailaddress)

                    Comment

                    • Adrienne Boswell

                      #11
                      Re: Replace commas in values in an array

                      Gazing into my crystal ball I observed "Evertjan."
                      <exjxw.hannivoo rt@interxnl.net writing in news:Xns990AC9A 31F8B5eejj99@
                      194.109.133.242 :
                      Adrienne Boswell wrote on 06 apr 2007 in
                      microsoft.publi c.inetserver.as p.general:
                      >
                      >
                      >I don't think that's going to work, here's why:
                      >>
                      >Let's say that request.form("n umbers") = "1, 2, 3, 4" and request.form
                      >("description" ) = "eggs, bacon, milk, butter"
                      >>
                      >>
                      >numbersarr = split(request.f orm("numbers"))
                      >
                      numbersarr = split(request.f orm("numbers"), ",")
                      I know, my fingers were going too fast.
                      >
                      >descarr = split(request.f orm("descriptio n"),",")
                      >
                      if ubound(numbersa rr)>=ubound(des carr) then
                      max = ubound(descarr)
                      else
                      max = ubound(numbersa rr)
                      end if
                      for i = 0 to max - 1
                      ....
                      >
                      >for i = 0 to ubound(numbersa rr)
                      > response.write numbersarr(i) & "=" & descarr(i)
                      >next
                      >>
                      >That works if the arrays are both the same size. If request.form
                      >("description" ) = "eggs, milk, butter, sugar, flour" then you've got a
                      >problem.
                      >
                      See serverside solution above.
                      >
                      >The form I am making has 10 rows of 5 items each, so that's why I'm
                      >doing it client side.
                      >
                      ??
                      >
                      Like this:
                      <% for i = 0 to 10%>
                      <tr>
                      <td><input name="desc" id="desc<%=i%> " type="text"></td><td><input
                      type="text" name="numbers" id="numbers<%=i %>"></td>
                      </tr>
                      <% next%>
                      <input type="submit" value="Submit">
                      </form>

                      Without entering anything, but submitting the values would be:
                      desc = ,,,,,,,,,
                      numbers = ,,,,,,,,,

                      Multiple values for the same form element are separated by commas, ergo
                      the problem. If one of the values being returned has a comma in it, then
                      it will throw the count off, hence the need for the client side change.

                      --
                      Adrienne Boswell at Home
                      Arbpen Web Site Design Services
                      Arbpen Consulting will help you harness valuable insights and translate them into tangible results by merging data and strategy.

                      Please respond to the group so others can share

                      Comment

                      • Evertjan.

                        #12
                        Re: Replace commas in values in an array

                        Adrienne Boswell wrote on 07 apr 2007 in
                        microsoft.publi c.inetserver.as p.general:

                        [...]
                        >>The form I am making has 10 rows of 5 items each, so that's why I'm
                        >>doing it client side.
                        >>
                        >??
                        >>
                        >
                        Like this:
                        <% for i = 0 to 10 %>
                        11 times ;-)
                        <tr>
                        <td><input name="desc" id="desc<%=i%> " type="text"></td><td><input
                        type="text" name="numbers" id="numbers<%=i %>"></td>
                        </tr>
                        <% next%>
                        <input type="submit" value="Submit">
                        </form>
                        >
                        Without entering anything, but submitting the values would be:
                        desc = ,,,,,,,,,
                        numbers = ,,,,,,,,,
                        try:

                        ========= test1.asp ===============
                        <% response.write "Responses: " & request.form("d esc") %>

                        <form method='post'>
                        <input name='desc'>
                        <input name='desc'>
                        <input name='desc'>
                        <input name='desc'>
                        <input name='desc'>
                        <input type='submit'>
                        </form>
                        =============== =============== ==
                        Multiple values for the same form element are separated by commas,
                        So it seems, but it is not comletely true, because:

                        ========= test2.asp ===============
                        <% response.write "Responses: " & request.form("d esc").count %>

                        <form method='post'>
                        <input name='desc'>
                        <input name='desc'>
                        <input name='desc'>
                        <input name='desc'>
                        <input name='desc'>
                        <input type='submit'>
                        </form>
                        =============== =============== ==

                        This responds: 5, independent of any filling of the fields at submission,
                        with or without commas.
                        so clearly the request.form("d esc") is NOT a string but a collection,
                        in test1 only converted to a string by response.write.
                        ergo the problem. If one of the values being returned has a comma in
                        it, then it will throw the count off, hence the need for the client
                        side change.
                        Now we know it is a collection, the solution is near:

                        ========= test3.asp ===============
                        <%
                        for i=1 to request.form("d esc").count
                        response.write i & ": " & request.form("d esc")(i) & "<br>"
                        next
                        %>

                        <form method='post'>
                        <input name='desc' value='1qwe,ert '>
                        <input name='desc' value='2asd,ert '>
                        <input name='desc' value='3zxc,poi '>
                        <input name='desc' value='4qwe,xxx '>
                        <input name='desc' value='5poi,ert '>
                        <input type='submit'>
                        </form>
                        =============== =============== ==

                        The commas do not interfere with the count,
                        in this fully serverside solution.

                        --
                        Evertjan.
                        The Netherlands.
                        (Please change the x'es to dots in my emailaddress)

                        Comment

                        • Dave Anderson

                          #13
                          Re: Replace commas in values in an array

                          "Adrienne Boswell" wrote:
                          <% for i = 0 to 10%>
                          <tr>
                          <td><input name="desc" id="desc<%=i%> " type="text"></td><td><input
                          type="text" name="numbers" id="numbers<%=i %>"></td>
                          </tr>
                          <% next%>
                          Let me make something clear that you may not have considered: the client is
                          under no obligation to keep those name-value pairs in order -- and
                          frequently sends them in a different order than they appear. That means this
                          approach will fail whether you change commas to semicolons or not.

                          A much better approach is to uniquely *name* the elements:

                          <% For i = 0 To 10 %>
                          <input name="desc<%=i% >" ...>
                          <% Next %>


                          You can then examine the form contents like this:

                          For i = 0 To 10
                          DoStuffWith(Req uest.Form("desc " & i))
                          Next




                          --
                          Dave Anderson

                          Unsolicited commercial email will be read at a cost of $500 per message. Use
                          of this email address implies consent to these terms.

                          Comment

                          • scripts.contact

                            #14
                            Re: Replace commas in values in an array


                            Adrienne Boswell wrote:
                            When the data gets returned it's something like:
                            header: "socks, shoes, pizza, cats, dogs"
                            description: "good for feet, wear with socks, cheese only, have lots
                            of hair, bark a lot"
                            >
                            Problem comes when the description or header have a comma. I need a
                            little javascript that will replace the comma with a semicolon
                            someVar = someVar.replace (/,/g,';')

                            Comment

                            • Pete

                              #15
                              Re: Replace commas in values in an array

                              , = &#44;

                              On Apr 6, 6:02 am, "Adrienne Boswell" <arb...@yahoo.c omwrote:
                              Although this is a client side issue, I am also posting to asp.general
                              in case there is someway to do this only server side (which I would
                              prefer).
                              >
                              Here's my form:
                              >
                              <form method="post" action="<%=requ est.servervaria bles("Script_na me")
                              %>"
                              <% for i - 0 to 4%>
                              Header: <input type="text" name="header" id="header<%=i% >"
                              onblur="functio n(this)"><br>
                              Description: <input type="text" name="descripti on" id="description <%=i
                              %>" onblur="functio n(this)">
                              <% next%>
                              <input type="submit" value="Submit">
                              </form>
                              >
                              When the data gets returned it's something like:
                              header: "socks, shoes, pizza, cats, dogs"
                              description: "good for feet, wear with socks, cheese only, have lots
                              of hair, bark a lot"
                              >
                              Problem comes when the description or header have a comma. I need a
                              little javascript that will replace the comma with a semicolon client
                              side before it gets to
                              the server, preferably as they leave the field.
                              >
                              Any help with this would be much appreciated.
                              >
                              --
                              Adrienne Boswell at work
                              Administrator nextBlock.com

                              Please respond to the group so others can share

                              Comment

                              Working...