/    Sign up×
Community /Pin to ProfileBookmark

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?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@SMTSSep 03.2011 — 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;
}
[/CODE]


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
}[/CODE]


Good luck.
Copy linkTweet thisAlerts:
@Robert_BrooksauthorSep 04.2011 — 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>
Copy linkTweet thisAlerts:
@SMTSSep 08.2011 — 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.
×

Success!

Help @Robert_Brooks spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...