473,321 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

How to format textbox value before sum?

I'm looping through some textboxes to sum the values. If the value in
the textbox has a dot or a space as thousand separator the sum function
doesn't work.

Can someone help me to make it work also with thousand separators?

function UpdateBudget(callingcontrol, totalcontrol)
{
var amount = 0
var f = document.forms['form1'];
if (f) {
f = f.elements;
}
var i = f.length;
var disp = ""
while (i-- > 0) {
if (f[i].type == 'text') {
if (f[i].name.indexOf("Budget") >= 0) {
if (f[i].name.substr(0,16) ==
callingcontrol.name.substr(0,16)) {

var n = parseInt(f[i].value, 10);
if (isNaN(n)) {
amount += 0;
}
else {
amount += n;
}
}
}
}
}

SumBudget(callingcontrol, amount)
}

Grateful for help!

Regards,

S

Mar 7 '06 #1
15 2412
st****@gmail.com said the following on 3/7/2006 1:32 AM:
I'm looping through some textboxes to sum the values. If the value in
the textbox has a dot or a space as thousand separator the sum function
doesn't work.
What about a comma separator?
Can someone help me to make it work also with thousand separators?
<snip>
var n = parseInt(f[i].value, 10);


var n = +f[i].value.replace(' ','').replace('.','').replace(',','');

Will remove any spaces, commas or periods (dot). Makes me wonder what
happens if it has a decimal part to it though......

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 7 '06 #2
Randy Webb wrote:
var n = +f[i].value.replace(' ','').replace('.','').replace(',','');

Will remove any spaces, commas or periods (dot). Makes me wonder what
happens if it has a decimal part to it though......


var s = '.. ,, xx';

alert(s.replace(' ','').replace('.','').replace(',',''));
alert(+s.replace(' ','').replace('.','').replace(',',''));

I'm totally sure you know that the replace isn't recursive with single
strings, but as you love to find such idiot mistakes on the people code,
I showed your one too, but I won't do it again, since this is a really
idiot behaviour, makes me remember of our friend Mr. Spock :b
--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
Mar 7 '06 #3
Jonas Raoni said the following on 3/7/2006 2:31 AM:
Randy Webb wrote:
var n = +f[i].value.replace(' ','').replace('.','').replace(',','');

Will remove any spaces, commas or periods (dot). Makes me wonder what
happens if it has a decimal part to it though......


var s = '.. ,, xx';

alert(s.replace(' ','').replace('.','').replace(',',''));
alert(+s.replace(' ','').replace('.','').replace(',',''));

I'm totally sure you know that the replace isn't recursive with single
strings, but as you love to find such idiot mistakes on the people code,
I showed your one too, but I won't do it again, since this is a really
idiot behaviour, makes me remember of our friend Mr. Spock :b


I don't hunt "idiot mistakes", but I do point them out when I see them.
And yes, you are correct.

One thing I *do* do when I point it out is to offer a solution, and when
corrected I accept it:

var n= +f[i].s.replace(/\./g, '').replace(/\,/g, '').replace(/\ /g, '');

But you already knew that, right?

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 7 '06 #4
Randy Webb wrote:
I don't hunt "idiot mistakes", but I do point them out when I see them.
But it's horrible when you point them out, anybody is perfect, the
reader must be able to find the little mistake and correct by himself,
the important is the idea behind the code ='/
And yes, you are correct.
One thing I *do* do when I point it out is to offer a solution, and when
corrected I accept it:

var n= +f[i].s.replace(/\./g, '').replace(/\,/g, '').replace(/\ /g, '');

But you already knew that, right?


Sure, but I don't like to correct these mistakes, I consider that you
have a nice knowledge, so I take the mistakes as simple distractions
that can be seen by anyone, so they don't need to be corrected, but it's
just my opinion :b

It's annoying to stay correcting and correcting, your code isn't perfect
yet for example:

alert(+"a".replace(/\./g, '').replace(/\,/g, '').replace(/\ /g, ''));

But again, I won't correct and you don't need to do it, you already
pointed out the solution, let the person who asked dig a little :]

Now I'm really sleepy, it's 5am here haha, I'm tired of writing =/

Take a look on my recent movie
<URL:http://youtube.com/watch?v=lnQTZxqxc10> haha \o/\o/\o/

See ya tomorrow, there are many things to be discussed yet >=]
--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
Mar 7 '06 #5
Jonas Raoni said the following on 3/7/2006 3:14 AM:
Randy Webb wrote:
I don't hunt "idiot mistakes", but I do point them out when I see them.


But it's horrible when you point them out, anybody is perfect, the
reader must be able to find the little mistake and correct by himself,
the important is the idea behind the code ='/


No, you have it backwards. Bad code *needs* to be pointed out and
especially to new people. If it's not then the newbe's learn bad coding
and have to re-learn it and in the process the answers, and quality of
those answers, in comp.lang.javascript diminishes. There is no good to
letting bad answers go and nothing but good to come of correcting them.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 7 '06 #6
JRS: In article <B6********************@comcast.com>, dated Tue, 7 Mar
2006 01:54:30 remote, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :

var n = +f[i].value.replace(' ','').replace('.','').replace(',','');

Will remove any spaces, commas or periods (dot). Makes me wonder what
happens if it has a decimal part to it though......


In a function UpdateBudget, one should not expect decimal parts. "A
billion here, a billion there - it soon adds up to real money" (approx).

Assuming your answer to have only the obvious answer-affecting error,

var n = +f[i].value.replace(/[\. ,]/g, "")

in which, in my system, the \ is not actually needed.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Mar 7 '06 #7
Dr John Stockton wrote:
Assuming your answer to have only the obvious answer-affecting error,

var n = +f[i].value.replace(/[\. ,]/g, "")

in which, in my system, the \ is not actually needed.


It is never necessary to escape the literal dot character within a
character class in a Regular Expression.
PointedEars
Mar 8 '06 #8
I'm grateful for the answers but they none of them are working on my
system. One textbox contains for example the value "2 100". This should
be treated as "2100" but is treated like "NaN".

Maybe it's easier to say that space, dot or comma should be eliminated.
And if the user by mistake writes anything else than numbers it should
be treated like "0" so that they always get a correct sum.

Thanks again!

/ S

Mar 14 '06 #9
st****@gmail.com said the following on 3/14/2006 7:55 AM:
I'm grateful for the answers but they none of them are working on my
system. One textbox contains for example the value "2 100". This should
be treated as "2100" but is treated like "NaN".

Maybe it's easier to say that space, dot or comma should be eliminated.
<input onchange="
this.value=this.value.replace(/\./g,'').replace(/\,/g,'').replace(/\ /g,
'');"

Will remove any commas, any dots and any spaces from the input. Test it :)
And if the user by mistake writes anything else than numbers it should
be treated like "0" so that they always get a correct sum.


So, if I type in "Randy Webb" instead of 123, it should deduce that I
meant 0? Test it for all digits after removing spaces commas and
periods. Then, if it contains non-digits you tell the user they have
incorrect input.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 14 '06 #10
st****@gmail.com wrote:
I'm grateful for the answers but they none of them are working on my
system. One textbox contains for example the value "2 100". This should
be treated as "2100" but is treated like "NaN".
Not with Mr Stockton's code, whether the redundant backslash is omitted
or not.
Maybe it's easier to say that space, dot or comma should be eliminated.
Which is what his code does.
And if the user by mistake writes anything else than numbers it should
be treated like "0" so that they always get a correct sum.


var v = f[i].value;

if (v.test(/[^\s\d.,]/))
{
v = 0;
}
else
{
v = +v.replace(/[\s.,]+/g, "");
}
PointedEars
Mar 14 '06 #11
VK

Thomas 'PointedEars' Lahn wrote:
st****@gmail.com wrote:
I'm grateful for the answers but they none of them are working on my
system. One textbox contains for example the value "2 100". This should
be treated as "2100" but is treated like "NaN".


Not with Mr Stockton's code, whether the redundant backslash is omitted
or not.
Maybe it's easier to say that space, dot or comma should be eliminated.


Which is what his code does.
And if the user by mistake writes anything else than numbers it should
be treated like "0" so that they always get a correct sum.


var v = f[i].value;

if (v.test(/[^\s\d.,]/))
{
v = 0;
}
else
{
v = +v.replace(/[\s.,]+/g, "");
}


IMHO it is a pure internationalisation question. In the US comma is
used as "readability separator" for long numbers: 1,000,000 plus
decimal point for floating part: 1,000.99. In some European countries
and in Russia a space is used for long numbers: 1 000 000 and decimal
comma for floating part: 1 000,99

Unless one knows exactly in what locale the number is typed, no RegExp
can help to parse a number properly. That should be two ways then:
1. Have separate "clean up" subs for different locales plus a sub to
determine the current locale with at least 90% of guarantee. (Actually
what could it be to check?)

2. Simply state in your form what number format do you expect and
accept only this format (most forms are using 2dn approach so far
AFAICT).

Mar 14 '06 #12
JRS: In article <11*********************@j52g2000cwj.googlegroups. com>,
dated Tue, 14 Mar 2006 06:25:13 remote, seen in
news:comp.lang.javascript, VK <sc**********@yahoo.com> posted :
In some European countries
and in Russia a space is used for long numbers: 1 000 000 and decimal
comma for floating part: 1 000,99


The space is also recommended in international standards : not
necessarily ISO, but at least IUPAP/SUNAMCO. IIRC, it should be a Thin
Space, which poses difficulties in ASCII - does UniCode have one?

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Mar 14 '06 #13
Dr John Stockton wrote:
[...] VK <sc**********@yahoo.com> posted :
In some European countries
and in Russia a space is used for long numbers: 1 000 000 and decimal
comma for floating part: 1 000,99


The space is also recommended in international standards : not
necessarily ISO, but at least IUPAP/SUNAMCO. IIRC, it should be a Thin
Space, which poses difficulties in ASCII - does UniCode have one?


U+2009 (THIN SPACE) or U+200A (HAIR SPACE).

I found it through

<URL:http://en.wikipedia.org/wiki/Space_%28punctuation%29>

and

<URL:http://ppewww.ph.gla.ac.uk/~flavell/unicode/unidata20.html>

It can also be found on

<URL:http://www.unicode.org/charts/charindex2.html>

and

<URL:http://www.unicode.org/charts/charindex3.html>

(I missed the links on top at first.)
PointedEars
Mar 15 '06 #14
I'm using:

var n = f[i].value.replace(/\./g,'').replace(/\,/g,'').replace(/\ /g,
'');
alert(n);

And n is still "2 100" when it should be "2100". I can't for my life
understand why. I'm using ASP.NET 2.0 and when I use ASP.NET 1.1 it
works...

Mar 15 '06 #15
st****@gmail.com wrote:
I'm using:

var n = f[i].value.replace(/\./g,'').replace(/\,/g,'').replace(/\ /g,
'');
alert(n);

And n is still "2 100" when it should be "2100". I can't for my life
understand why. I'm using ASP.NET 2.0 and when I use ASP.NET 1.1 it
works...


I do not understand this either. First, neither the comma nor the space
character needs to be escaped in a regular expression, but if they are
escaped, the escape sequence could have a special meaning, depending on
the RegExp's flavor. Second, `alert(n)' indicates client-side script code
(window.alert), so whether or not you use ASP(.NET) to _generate_ that code
(if that really is what the client gets) is irrelevant. And you should try
the code I posted first.

And, last but not least: Will you learn to post? NetNews is thread-based.
Reply to what you are referring to, and quote the minimum of that:

<URL:http://jibbering.com/faq/faq_notes/pots1.html#ps1Post>
<URL:http://www.safalra.com/special/googlegroupsreply/>
PointedEars
Mar 15 '06 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: ST | last post by:
Hello, I will be posting 2 errors that I just can't seem to figure out (I'll put them in different posts). I have looked all over the internet, and I still can't figure these out! This webapp was...
8
by: Assimalyst | last post by:
Hi i have a value entered into an asp text box, procedureDateTxtBx.Text, thet has the format dd/MM/yyyy. I need to convert this into a format recognisable by SQL server in order to properly query...
4
by: Jan Nielsen | last post by:
Hi I have a dataform that shows a date from a MSDE 2000 in a textbox. It uses the format: dd-mm-yyyy hh:mm:ss It would it to use the format dd-mm-yyyy But I can't find a Format property for the...
3
by: Slonocode | last post by:
I have some textboxes bound to an access db. I wanted to format the textboxes that displayed currency and date info so I did the following: Dim WithEvents oBidAmt As Binding oBidAmt = New...
1
by: Rich | last post by:
Hello, I have some datefields in a dataset (ds1). I bind some textbox controls on a windows form to these date fields in ds1, but I only want to see 01/01/2004 instead of 1/1/2004 8:00:00 AM. ...
4
by: Art | last post by:
Hi, I have a MyTextBox class that inherits from TextBox. How do I make it display the information as, say 0.00. Sometimes the value will be typed by the user. Other times the value will be...
7
by: Richiep | last post by:
I am trying to get a UK format date of dd/mm/yyyy. Why does the following subroutine not return a valid date in a web form? The date returned is #12:00:00 AM# but the date I entered into the...
11
by: RipperT | last post by:
Don't know if this group covers web apps, but here goes. In VS 2005, I am trying to get variables to hold thier values during postback from the server. I convert a text box's user-keyed value to an...
9
by: Mark G. | last post by:
Good afternoon. I have a "Fixed/2" format applied to a textbox on its property sheet. When I type data in the textbox, it is formatted correctly. But when I update the value programatically by...
7
by: pamela fluente | last post by:
My numericUpDowns show numbers in the format 1.500,56 (italy). Instead, I need *invariantly* that they show and accept the format 1,500.56, as in the USA. What's the right way to do that? I...
0
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
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...
1
isladogs
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...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.