hey there...
i'm trying to format a 6 digit dollar amount to include a comma ($1,240) and a period ($1,240.50)... i have the period solved thru:
form.total.value = number.toFixed(2);
but how is it possible to add a comma in a dollar amount that is over one thousand dollars?
9 1952
See this page. It should be what you want. They also have an advanced NumberFormat object which offers complex number formatting.
this doesn't seem to work... what am i doing wrong? -
<head>
-
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
-
<title>Untitled Document</title>
-
<script type="text/javascript">
-
function addCommas(nStr)
-
{
-
nStr += '';
-
x = nStr.split('.');
-
x1 = x[0];
-
x2 = x.length > 1 ? '.' + x[1] : '';
-
var rgx = /(\d+)(\d{3})/;
-
while (rgx.test(x1)) {
-
x1 = x1.replace(rgx, '$1' + ',' + '$2');
-
}
-
return x1 + x2;
-
}
-
</script>
-
</head>
-
-
<body>
-
<label>
-
<input type="text" name="textfield" onBlur="addCommas(this.nStr);">
-
</label>
-
</body>
-
</html>
-
It won't work because the input object has no attribute nStr. You have to pass the value: - onBlur="addCommas(this.value);"
It won't work because the input object has no attribute nStr. You have to pass the value: - onBlur="addCommas(this.value);"
this doesn't work either... -
<head>
-
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
-
<title>Untitled Document</title>
-
<script type="text/javascript">
-
function addCommas(nStr)
-
{
-
nStr += '';
-
x = nStr.split('.');
-
x1 = x[0];
-
x2 = x.length > 1 ? '.' + x[1] : '';
-
var rgx = /(\d+)(\d{3})/;
-
while (rgx.test(x1)) {
-
x1 = x1.replace(rgx, '$1' + ',' + '$2');
-
}
-
return x1 + x2;
-
}
-
</script>
-
</head>
-
-
<body>
-
<label>
-
<input type="text" name="textfield" onBlur="addCommas(this.value);">
-
</label>
-
</body>
-
</html>
-
i don't think i clearly understand yet when the event handeler (?i think that's what it's called) calls a functions.... like i usually match "();" with "()", and "(this.form);" with "(form)" and "(this.value);" with "(val)"... i'm sure i missed something along with way of learning javascript...
You need to set the value too: - onBlur="this.value=addCommas(this.value);"
Although it shouldn't affect the input box display, you should include it within form tags.
ok that worked... thanks...
one last question concerning this issue... is it possible to combine the AddCommas function to another one instead of using the onChange event handeler? the reason is because the number text fields are generated by select menus, not by actual typing input... i realize i could just add onChange event handlers to the select menus, but i think i'd prefer combine the two functions, to make the code tighter (i guess?)... plus it would be nice to learn :-)
i would like to combine the following two functions... and have the AddCommas function work for the:
A) form.invoicesubtotal.value
B) form.total2.value -
function InvoiceSubtotalTaxTotal(form)
-
{
-
var a = (form.assignmentinvoice.value != '') ? eval(form.assignmentinvoice.value) : 0;
-
var b = (form.printinginvoice.value != '') ? eval(form.printinginvoice.value) : 0;
-
var invoicesubtotalVAR = a + b;
-
form.invoicesubtotal.value = '$' + invoicesubtotalVAR.toFixed(2) + ' CAD';
-
var SubTotal = invoicesubtotalVAR;
-
var GST = SubTotal * 0.06;
-
form.gst.value = GST.toFixed(2);
-
var QST = (SubTotal + GST) * 0.075;
-
form.qst.value = QST.toFixed(2);
-
var Shipping = (form.shippinginvoice.value != '') ? eval(form.shippinginvoice.value) : 0;
-
form.shippinginvoice.value = Shipping.toFixed(2);
-
var TOTAL = SubTotal + GST + QST + Shipping;
-
form.total2.value = '$' + TOTAL.toFixed(2) + ' CAD';
-
}
-
-
function addCommas(nStr)
-
{
-
nStr += '';
-
x = nStr.split('.');
-
x1 = x[0];
-
x2 = x.length > 1 ? '.' + x[1] : '';
-
var rgx = /(\d+)(\d{3})/;
-
while (rgx.test(x1)) {
-
x1 = x1.replace(rgx, '$1' + ',' + '$2');
-
}
-
return x1 + x2;
-
}
-
Keep both functions separate (easier to read, better modularity, one task- one function, general functions can be used in other places too, etc.)
Call the addCommas function from within the InvoiceSubtotalTaxTotal function, e.g. on total2: - form.total2.value = '$' + addCommas(TOTAL.toFixed(2)) + ' CAD';
Keep both functions separate (easier to read, better modularity, one task- one function, general functions can be used in other places too, etc.)
Call the addCommas function from within the InvoiceSubtotalTaxTotal function, e.g. on total2: - form.total2.value = '$' + addCommas(TOTAL.toFixed(2)) + ' CAD';
great! works perfectly... thanks again acoder :-)
No problem, you're welcome.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: bdog4 |
last post by:
I want to validate a number to so that they can only input $2.00, 5.00,
7.00, 50.00 etc... I don't wan them to be able to put cents on the
amount. It can either either be 2 or 2.00 Any ideas on...
|
by: mark.a.lemoine |
last post by:
I need to be selecting records based on the value of a string-format
currency value.
Specifically, I have a table with a field of type varchar(50). Stored
in this field is a dollar-formatted...
|
by: tshad |
last post by:
I need to convert a number I get from my Sql table (where the value is a
string) to a string and have it be a precision of 2 and I want to reverse
the sign.
I originally used:
...
|
by: Digital Puer |
last post by:
I've inherited some code where the coder placed dollar signs in
his preprocessor macros. What is the significance of the
dollar signs ($) ?
Example:
#define ALLOCATE(task,pointer) \
{ \...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |