SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Mortgage Calculator - Need to Strip Commas

    I have a javascript mortgage calculator that is working fine, except it does not allow you to input values with commas in it. How do I strip commas out of the entered values? Here's my code:

    <script>
    // calculates the payments and updates for fields to
    // display the result
    function calcpayment() {
    var amt = document.frm.amt.value;
    var ir = document.frm.ir.value / 1200;
    var term = document.frm.term.value * 12;
    var total=1;
    for (i=0;i<term;i++) {
    total = total * (1 + ir);
    }
    mp = amt * ir / ( 1 - (1/total));
    // use Math object to chop all numbers after 2 digits
    document.frm.payment.value = Math.round(mp*100)/100;
    document.frm.total.value = Math.round(mp * term *100)/100 ;
    }
    </script>
    <h3 style="text-align: center; color: black">Mortgage Calculator</h3>
    <center>
    <form name="frm">
    <table bgcolor="#333333" cellpadding="0">
    <tr><td>
    <table bgcolor="#CBDFAA" cellspacing="10">
    <tr>
    <td>Mortgage amount</td>
    <td><input type="text" name="amt" /></td>
    </tr>
    <tr>
    <td>Yearly interest rate</td>
    <td><input type="text" name="ir" /></td>
    </tr>
    <tr>
    <td>Term (in years)</td>
    <td><input type="text" name="term" /></td>
    </tr>
    <tr><td colspan="2"><hr/></td></tr>
    <tr>
    <td>Monthly payment/td>
    <td><input type="text" name="payment" /></td>
    </tr>
    <tr>
    <td>Total payments/td>
    <td><input type="text" name="total" /></td>
    </tr>
    <tr >
    <td width="100%" height="31" colspan="2">
    <input type="button" value="CALCULATE MONTHLY PAYMENT"
    onclick="calcpayment();" />
    </td></tr>
    </table>
    </td></tr></table>
    </form>
    </center>

  2. #2
    SitePoint Zealot
    Join Date
    Jul 2006
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For example:

    Code:
    amt = amt.replace(",", "");
    Also you may just want to do something like this instead:

    Code:
    var amt = document.frm.amt.value;
    if (!parseInt(amt)) {
        alert("You must enter a whole number for your loan amount!");
        return;
    }

  3. #3
    SitePoint Evangelist SpaceFrog's Avatar
    Join Date
    Jan 2005
    Location
    Outerspace
    Posts
    511
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I always get mixed up with your notation... is the comma the thousand or the decimal separator ?
    for the thousand try an parseFloat
    for the deciamls depending if you want to round ceil or floor or even just get ird of decimals => toFixed

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Here the space is the thousands separator and the period is the decimal point and commas shouldn't be used at all since it is confusing as to which is meant. Most people still use commas instead of spaces though.

    To strip all of the commas (as thousands separators) from the string in one go use:

    amt = amt.replace(/,/g, "");
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Evangelist SpaceFrog's Avatar
    Join Date
    Jan 2005
    Location
    Outerspace
    Posts
    511
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Over here space or point is the thousand separator and comma is for decimals ...

    Gee regExp I just can't get around them ... would you know of a really basic tutorial for regExp patterns ?

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    My tutorial series on regular expressions can be found at http://javascript.about.com/od/regul...xpressions.htm
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  7. #7
    SitePoint Evangelist SpaceFrog's Avatar
    Join Date
    Jan 2005
    Location
    Outerspace
    Posts
    511
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks a bunch !

  8. #8
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This post deals with the same thing.


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
  •