473,545 Members | 2,793 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(ca llingcontrol, 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,1 6)) {

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

SumBudget(calli ngcontrol, amount)
}

Grateful for help!

Regards,

S

Mar 7 '06 #1
15 2421
st****@gmail.co m 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.javas cript 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.replac e(' ','').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.replac e(' ','').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.javas cript 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".repl ace(/\./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=lnQTZxq xc10> 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.javas cript diminishes. There is no good to
letting bad answers go and nothing but good to come of correcting them.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 7 '06 #6
JRS: In article <B6************ ********@comcas t.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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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.co m 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.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 14 '06 #10

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

Similar topics

0
1005
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 working fine, and then all of a sudden it just "broke"! Thanks in advance for your help! Input string was not in a correct format....
8
12998
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 the database. I've tried a few DateTime.Parse methods but can't get any to work. The format of these values in the database is yyyy-MM-dd...
4
5333
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 textbox. So I tried in the VB code the wizard generates Me.editDateOfBirth.DataBindings.Add(New System.Windows.Forms.Binding("Text",...
3
3076
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 Binding("Text", Me.Ds1, "Items.BidAmt") txtBidAmt.DataBindings.Add(oBidAmt) Private Sub oBidAmt_Format(ByVal sender As Object, ByVal e As...
1
9394
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. In a DateTimePicker control I can set a custom format("MM/dd/yyyy"). So when I iterate through ds1 with currency manager (cma) I see the correct...
4
7977
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 filled in when a User makes a selection elsewhere. I'd appreciate any help. Art
7
6808
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 text box was 24/06/2006. The other solution I have tried is given by the following two lines that do not compile because a value of date type...
11
2237
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 integer and assign it to a module level variable, then convert the variable and assign it to a hidden text box, setting it's EnableViewState to...
9
9299
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 setting the "value" property, the text does not format. I can get around this of course by using the VBA format() command. But is there any way I...
7
4534
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 have tried to set the current culture to the US culture, but nothing changed. For instance I tried without success (vb) :
0
7432
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7689
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7943
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7456
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3490
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1919
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.