SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist elgumbo's Avatar
    Join Date
    Nov 2002
    Location
    North West, UK
    Posts
    545
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    remove a comma from a value ?

    Hi

    I have a shopping cart on a site that formats the cost with a comma if the value is over 1000. eg 1,365

    I am trying to set up a third party affiliate tracking system to work with the cart. The tracking javascript collects both the value and the order number and users a comma to separtate the two.

    eg (total,orderid)

    This works fine when the order is under 1000 but once I hit that magic number the comma messes up the script because now the script only sees the first number before the comma as the total and the rest of the total as the order number. It disregards the real orderid completely.

    eg (1,365)

    Is there anyway I can take the order value and strip out the comma before passing it to the javascript?

    I have contacted the people behind both the cart and the tracker and neither can help. The cart has to put a comma in and the tracker has to have a comma delimiter. This is not my site so I'm stuck with both of them trying to figure out a solution.

    Any help appreciated.

  2. #2
    SitePoint Member
    Join Date
    Jun 2003
    Location
    Manchester, UK
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you should be able to use the 'replace' method of String. However, I'm having a similar problem trying to replace all the commas in a postal address with \n's.

    var strAddress = new String(Request.QueryString("ADDRESS"));
    strAddress.replace(/,/, '\n');

    Any clues why this doesn't work...?

  3. #3
    SitePoint Addict
    Join Date
    May 2000
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe this example will help:
    <html>
    <head>
    <title>Remove Commas</title>
    <script language="JavaScript">
    function Filternum()
    {
    re = /,/g;
    numonly=document.myform.numfield.value.replace( re,"")
    document.myform.numfield.value=numonly
    }
    </script>
    </head>
    <body>
    <form name="myform">
    <input type="text" name="numfield" size="10">
    <input type="button" value="Remove Commas" onClick="Filternum()">
    </form>
    </body>
    </html>

  4. #4
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The patterns work, but if this is just numbers, you can use parseInt() as well.

    var numAsString = "1,234";
    var numAsNum = parseInt( numAsString, 10 );

    *The regex method*
    var numAsNum = Number( numAsString.replace( /\D/g, "" ) );
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  5. #5
    SitePoint Evangelist elgumbo's Avatar
    Join Date
    Nov 2002
    Location
    North West, UK
    Posts
    545
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks everyone,

    I'll give them a go and see what happens


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •