Remove commas from dollar amounts in JavaScript using a split
Have you ever tried to add up multiple fields with JavaScript, only to find out that the client entered a comma to separate thousands and millions? This causes JavaScript to drop all of the digits after the comma. SearchDomino.com member Mike Parman explains how to use the split command to extract the commas prior to adding the fields together.
VIEW MEMBER FEEDACK TO THIS TIP
Have you ever tried to add up multiple fields with JavaScript, only to find out that the client entered a comma to separate thousands and millions? This causes JavaScript to drop all of the digits after the comma. You can use the split command to extract the commas prior to adding the fields together.
Add the removeComma function to the JavaScript Header section of your form, then add the call to the function, onto the onBlur section. Now, when the client moves from one field to another, all commas will be removed from the field.
function removeComma(num) { var temp1 = 0; var xarray = num.split(","); if ( xarray[2] != null ) { temp1 = xarray[0]+xarray[1] +xarray[2]; return temp1 } else if ( xarray[1] != null ) { temp1 = xarray[0] +xarray[1]; return temp1} else {temp1 = xarray[0]; return temp1 } }
I've found this works as well.
function removeComma(num) { var temp1 = num; for (i=1;i<3;i++) { // pick limit high enough to find all possible commas temp1 = temp1.replace(",",""); } return temp1; }
—Michael N.
Do you have comments on this tip? Let us know.
Related information from SearchDomino.com:
- Learning Guide: JavaScript development
- FAQ: JavaScript advice
This tip was submitted to the SearchDomino.com tip library by member Mike Parman. Please let others know how useful it is via the rating scale below. Do you have a useful Lotus Notes, Domino, Workplace or WebSphere tip or code snippet to share? Submit it to our monthly tip contest and you could win a prize.