How to format textbox value before sum?

Discussion in 'Javascript' started by staeri@gmail.com, Mar 7, 2006.

1. Guest

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.type == 'text') {
if (f.name.indexOf("Budget") >= 0) {
if (f.name.substr(0,16) ==
callingcontrol.name.substr(0,16)) {

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

SumBudget(callingcontrol, amount)
}

Grateful for help!

Regards,

S

, Mar 7, 2006

2. Randy WebbGuest

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.

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

<snip>

> var n = parseInt(f.value, 10);

var n = +f.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/

Randy Webb, Mar 7, 2006

3. Jonas RaoniGuest

Randy Webb wrote:
> var n = +f.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';

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

Jonas Raoni, Mar 7, 2006
4. Randy WebbGuest

Jonas Raoni said the following on 3/7/2006 2:31 AM:
> Randy Webb wrote:
>> var n = +f.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';
>
>
> 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.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/

Randy Webb, Mar 7, 2006
5. Jonas RaoniGuest

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.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:

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

See ya tomorrow, there are many things to be discussed yet >=]

--
Jonas Raoni Soares Silva
http://www.jsfromhell.com

Jonas Raoni, Mar 7, 2006
6. Randy WebbGuest

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/

Randy Webb, Mar 7, 2006
7. Dr John StocktonGuest

JRS: In article <>, dated Tue, 7 Mar
2006 01:54:30 remote, seen in news:comp.lang.javascript, Randy Webb
<> posted :
>
>var n = +f.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).

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

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

--
<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.

Dr John Stockton, Mar 7, 2006
8. Thomas 'PointedEars' LahnGuest

Dr John Stockton wrote:

>
> var n = +f.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

Thomas 'PointedEars' Lahn, Mar 8, 2006
9. Guest

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, 2006
10. Randy WebbGuest

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/

Randy Webb, Mar 14, 2006
11. Thomas 'PointedEars' LahnGuest

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.value;

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

PointedEars

Thomas 'PointedEars' Lahn, Mar 14, 2006
12. VKGuest

Thomas 'PointedEars' Lahn wrote:
> 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.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).

VK, Mar 14, 2006
13. Dr John StocktonGuest

JRS: In article <>,
dated Tue, 14 Mar 2006 06:25:13 remote, seen in
news:comp.lang.javascript, VK <> 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?

--
<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.

Dr John Stockton, Mar 14, 2006
14. Thomas 'PointedEars' LahnGuest

Dr John Stockton wrote:

> [...] VK <> 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

Thomas 'PointedEars' Lahn, Mar 15, 2006
15. Guest

I'm using:

var n = f.value.replace(/\./g,'').replace(/\,/g,'').replace(/\ /g,
'');

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, 2006
16. Thomas 'PointedEars' LahnGuest

wrote:

> I'm using:
>
> var n = f.value.replace(/\./g,'').replace(/\,/g,'').replace(/\ /g,
> '');
>
> 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>