Replace commas in values in an array

Discussion in 'ASP General' started by Adrienne Boswell, Apr 5, 2007.

  1. 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="<%=request.servervariables("Script_name")
    %>"
    <% for i - 0 to 4%>
    Header: <input type="text" name="header" id="header<%=i%>"
    onblur="function(this)"><br>
    Description: <input type="text" name="description" id="description<%=i
    %>" onblur="function(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, Apr 5, 2007
    #1
    1. Advertisements

  2. Adrienne Boswell

    Evertjan. Guest

    Adrienne Boswell wrote on 06 apr 2007 in
    microsoft.public.inetserver.asp.general:
    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., Apr 5, 2007
    #2
    1. Advertisements

  3. Assuming from your example that you want the expression for your onblur
    handler, here is one way:

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

    Another:

    onblur="this.value=this.value.replace(/,/g,';')"
     
    Dave Anderson, Apr 5, 2007
    #3
  4. Dave Anderson wote:
    Dave, you're an officer, a genteman and a fine judge of women! Thanks
    so much - works perfectly!
     
    Adrienne Boswell, Apr 6, 2007
    #4
  5. Adrienne Boswell

    RobG Guest

    It is a server issue.

    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.

    Tell your users not to use commas and use normal validation
    techniques.

    Client scripting is unreliable, deal with it at the server.

    For luck, you can use something like:

    <input onblur="this.value=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.
     
    RobG, Apr 6, 2007
    #5
  6. you could do it in javascript using regular expression. i believe
    something like this would work:

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

    i'd probably go about it a different way, though. i'd give each
    <input> tag a unique name, so you dont have to worry about the values
    being squashing into one value.
     
    brunascle.maps, Apr 6, 2007
    #6
  7. Don't just believe it, try it. Then you will notice that you need a global
    flag.
     
    Dave Anderson, Apr 6, 2007
    #7
  8. I don't think that's going to work, here's why:

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


    numbersarr = split(request.form("numbers"))
    descarr = split(request.form("description"),",")
    for i = 0 to ubound(numbersarr)
    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.

    The form I am making has 10 rows of 5 items each, so that's why I'm doing
    it client side.
     
    Adrienne Boswell, Apr 6, 2007
    #8
  9. Adrienne Boswell

    Evertjan. Guest

    Adrienne Boswell wrote on 06 apr 2007 in
    microsoft.public.inetserver.asp.general:

    numbersarr = split(request.form("numbers"),",")
    if ubound(numbersarr)>=ubound(descarr) then
    max = ubound(descarr)
    else
    max = ubound(numbersarr)
    end if
    for i = 0 to max - 1
    ....
    See serverside solution above.
    ??
     
    Evertjan., Apr 6, 2007
    #9
  10. I know, my fingers were going too fast.
    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, Apr 7, 2007
    #10
  11. Adrienne Boswell

    Evertjan. Guest

    Adrienne Boswell wrote on 07 apr 2007 in
    microsoft.public.inetserver.asp.general:

    [...]
    11 times ;-)
    try:

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

    <form method='post'>
    <input name='desc'>
    <input name='desc'>
    <input name='desc'>
    <input name='desc'>
    <input name='desc'>
    <input type='submit'>
    So it seems, but it is not comletely true, because:

    ========= test2.asp ===============
    <% response.write "Responses: " & request.form("desc").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("desc") is NOT a string but a collection,
    in test1 only converted to a string by response.write.
    Now we know it is a collection, the solution is near:

    ========= test3.asp ===============
    <%
    for i=1 to request.form("desc").count
    response.write i & ": " & request.form("desc")(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., Apr 7, 2007
    #11
  12. someVar = someVar.replace(/,/g,';')
     
    scripts.contact, Apr 9, 2007
    #12
  13. Adrienne Boswell

    Pete Guest

    , = ,
     
    Pete, Apr 10, 2007
    #13
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.