SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Jul 2004
    Location
    Georgia
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Stripping unwanted text

    I have an automotive calculator (price, payments etc)

    When my site is updated, I have the price field in the calculator automatically filled.

    Anyone know a good, script to remove the $ and , ?

    If anyone could help I would appreciate it.

    Thanks in advance.


    WWolfe

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    var new_val = old_val.replace(/[$,]/g, '');
    ::: certified wild guess :::

  3. #3
    SitePoint Member
    Join Date
    Jul 2004
    Location
    Georgia
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Being new to javascript, I'm not sure where to put what you gave me Adios. I used code my boss found for me for the calculator we use.
    It is as follows (Not including the actual calculator form script)

    function checkNumber(input, min, max, msg) {

    msg = msg + " field has invalid data: " + input.value;

    var str = input.value;
    for (var i = 0; i < str.length; i++) {
    var ch = str.substring(i, i + 1)
    if ((ch < "0" || "9" < ch) && ch != '.') {
    alert(msg);
    return false;
    }
    }
    var num = 0 + str
    if (num < min || max < num) {
    alert(msg + " not in range [" + min + ".." + max + "]");
    return false;
    }
    input.value = str;
    return true;
    }

    function computeField(input) {

    if (input.value != null && input.value.length != 0)
    input.value = "" + eval(input.value);
    computeForm(input.form);
    }

    function myRound(val) {

    val = val * 100;
    val = Math.round(val);
    return val / 100;
    }
    function computeForm(form) {

    if (form.downpayment.value == null || form.downpayment.value.length == 0)
    form.downpayment.value = 0;

    if ((form.payments.value == null || form.payments.value.length == 0) ||
    (form.interest.value == null || form.interest.value.length == 0)||
    (form.principal.value == null || form.principal.value.length ==
    0)) {
    return;
    }

    if (!checkNumber(form.payments, 1, 84, "# of payments") ||
    !checkNumber(form.interest, 0, 28, "Interest") ||
    !checkNumber(form.principal, 100, 200000, "Principal") ||
    !checkNumber(form.downpayment, 0, form.principal.value, "Down payment")) {
    form.payment.value = "Invalid";
    return;
    }

    var i = form.interest.value;

    if (i == 0) {
    for (var j = 0; j < form.payments.value; j++)
    var returnpayment = ((form.principal.value - form.downpayment.value));
    //form.payment.value = Math.round((returnpayment/form.payments.value) + 1) + ".00";
    form.payment.value = myRound(returnpayment/form.payments.value);
    } else {
    //if (i >= 1.0) {
    i = i / 100.0;
    //form.interest.value = i;
    //}
    i /= 12;

    var pow = 1;
    for (var j = 0; j < form.payments.value; j++)
    pow = pow * (1 + i);
    var returnpayment = ((form.principal.value - form.downpayment.value) * pow * i) / (pow - 1);
    //form.payment.value = Math.round(returnpayment + 1) + ".00";
    form.payment.value = myRound(returnpayment);
    }
    }

    function clearForm(form) {
    form.payments.value = "";
    form.interest.value = "";
    form.principal.value = "";
    form.downpayment.value = "";

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You said "...I have the price field in the calculator automatically filled" - but there's nothing in the sample you posted that would format a currency string (add '$' and ',' to it). In addition, JavaScript generally manipulates page objects set with HTML, so a disconnected script isn't of much use. Post the relevant - if you can figure out which are! - sections of the page, or zip-'er-up and wait.
    ::: certified wild guess :::

  5. #5
    SitePoint Member
    Join Date
    Jul 2004
    Location
    Georgia
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Clarification of what I'm in need of.

    <td class="c1"><font color=#FFFFFF><b>Loan Amount</b></font><br>
    $<input type="TEXT" name="principal" size="12" value="{Price}" onchange="computeField(this)"><br>

    Here is where the Amount is loaded.

    I use a template for the vehicles, the program I use automatically fills in the fields ie 'value="{Price}"

    The value that it inputs would be something like '$12,500'.

    The calculator that I'm using considers $ and , as invalid data.

    So, what I'm looking for is away of removing the $ and ,.

    I hope this helps.

    WW

  6. #6
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    ............
    ............
    function computeField(input) {
    input.value = input.value.replace(/[$,]/g, '');
    if (input.value != null && input.value.length != 0)
    input.value = "" + eval(input.value);
    computeForm(input.form);
    }
    This should modify the field itself (visibly). If you wanted the displayed value to remain formatted, and a 'sanitized' version used for calculation, it'll require more modification. Didn't you want the output (all fields) to be formatted as well?
    ::: certified wild guess :::

  7. #7
    SitePoint Member
    Join Date
    Jul 2004
    Location
    Georgia
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats almost what I'm looking for,

    I think the biggest issue I have, is that the automation of the form fields,

    the template field value {Price} will be changed to $12,000 for example

    so when someone views the page they will see the price. Thats when I needed the $ and , stripped.

    I realize I'm going in circles, to much multitasking, not enough caffine....

    Would it be easier to just code it so the $ and , are just ignored when it computes the total? if thats possible.

  8. #8
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ...so when someone views the page they will see the price.
    That's your problem...those SOBs who keep viewing your page. I hate when that happens.

    This any better?
    Code:
    <td class="c1"><font color=#FFFFFF><b>Loan Amount</b></font><br>
    $<input type="TEXT" name="principal" size="12" value="{Price}" onchange="computeField(this)"><br>
    <script type="text/javascript">
    var f = document.forms[document.forms.length-1].
    var e = f.elements[f.elements.length-1];
    if (e) e.value=e.val.replace(/[$,]/g, '');
    </script>
    ::: certified wild guess :::


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
  •