How to format textbox value before sum?

S

staeri

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
 
R

Randy Webb

(e-mail address removed) 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?

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

Jonas Raoni

Randy said:
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';

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
 
R

Randy Webb

Jonas Raoni said the following on 3/7/2006 2:31 AM:
Randy said:
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';

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.s.replace(/\./g, '').replace(/\,/g, '').replace(/\ /g, '');

But you already knew that, right?
 
J

Jonas Raoni

Randy said:
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:

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:
> haha \o/\o/\o/

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

Randy Webb

Jonas Raoni said the following on 3/7/2006 3:14 AM:
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.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Tue, 7 Mar
2006 01:54:30 remote, seen in Randy Webb
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).

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

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

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

Thomas 'PointedEars' Lahn

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

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
 
S

staeri

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
 
R

Randy Webb

(e-mail address removed) 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.
 
T

Thomas 'PointedEars' Lahn

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
 
V

VK

Thomas said:
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).
 
D

Dr John Stockton

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

Thomas 'PointedEars' Lahn

Dr said:
[...] VK said:
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_(punctuation)>

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
 
S

staeri

I'm using:

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

Thomas 'PointedEars' Lahn

I'm using:

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top