www.webdeveloper.com
Results 1 to 4 of 4

Thread: total currency form cells with $

  1. #1
    Join Date
    Sep 2011
    Posts
    2

    total currency form cells with $

    I have an online form with 6 cells formatted for $ whether user types $ or not. I have a total cell (hidden) that needs to total those 6 cells. I always get the NaN in the total cell. I have tried it without auto $ formatting, but if user inputs $, than the NaN will appear. Is there no way to total cells that have or may have $s?

  2. #2
    Join Date
    Apr 2010
    Posts
    121

    Here you go:

    As I assume you know, NaN stands for "not a number". Because the computer cannot add a character (like a,b,or c), you cannot add dollar signs.

    To avoid this problem, you will need to recognize a dollar sign at the beginning and ignore it when it's there.

    To do this, you can use this code:

    (assuming 'num' is the value of the cell you are trying to deal with)
    Code:
    if(num.substring(0,1) == '$') {
    var num = num.substring(1,num.length)*1;
    }
    This checks if the first character is a '$'. If it is, it sets the new value of the string to the original value of the string, starting at the second character, therefore getting rid of the dollar sign which was the first character.

    Also, if you wish, you can validate this with regex:

    Code:
    var patt = /^\$?[0-9]+(,[0-9]{3})*(\.[0-9]{2})?$/gi;
    if(patt1.test(num) !== true) {
    alert("invalid"); // or something else
    return false; //die
    }
    Good luck.

  3. #3
    Join Date
    Sep 2011
    Posts
    2
    I have been able to strip the $ and "," from the user's input. Then the following calculates the total of the fields and the result has no $ and no "," in the field. I use an onFocus event handler on the Total_Income field. Is there something I can add to document.LDA.Total_Income.value that would put back the $ and ","? With my limited knowledge, everything I have tried results in the NaN.

    Thanks


    <script type="text/javascript">
    <!-- Calculates Total Borrower Income
    function startCalc(){
    interval = setInterval("calc()",1);
    }
    function calc(){
    one = document.LDA.Borrower_Income_1.value;
    two = document.LDA.Co_Borrower_Income_1.value;
    three = document.LDA.Borrower_Income_2.value;
    four = document.LDA.Co_Borrower_Income_2.value;
    five = document.LDA.Borrower_Income_3.value;
    six = document.LDA.Co_Borrower_Income_3.value;
    document.LDA.Total_Income.value = (one * 1) + (two * 1) + (three * 1) + (four * 1) + (five * 1) + (six * 1);
    }
    function stopCalc(){
    clearInterval(interval);
    }
    // End -->
    </script>

  4. #4
    Join Date
    Apr 2010
    Posts
    121

    Well...

    If you put a dollar sign in the field, and then process it, It will turn up NaN (Not a Number). The trick would be to do it before JavaScript gets a chance to process it.

    A more complex method, would be using CSS to put the dollar sign on top of the text area, but this would just make you and me miserable.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles