473,511 Members | 16,252 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getting values from a form as Number, not a String

I've run into this problem a couple of times. When I try to get a
value from a form text field and add them together like so…

a = document.myform.field1.value
b = document.myform.field2.value
c = a + b
document.myform.field3.value = c

It treats the values as a string and if the input numbers are 2 and 4
I get 24 and not 6 like I would expect.

I can do some crazy math to force it to recognize the numbers like
this…

c = (a*10/10) + (b*10/10)

and get the results I want, because when I use multiplication and
division, it recognizes the numbers as numbers. But dose anyone know
how to get it to recognizes the numbers in the first place.

Thanks
Kenny
Jul 20 '05 #1
8 3133
Kenny wrote:
I've run into this problem a couple of times. When I try to get a
value from a form text field and add them together like so…

a = document.myform.field1.value
b = document.myform.field2.value
c = a + b
document.myform.field3.value = c

It treats the values as a string and if the input numbers are 2 and 4
I get 24 and not 6 like I would expect.

I can do some crazy math to force it to recognize the numbers like
this…

c = (a*10/10) + (b*10/10)

and get the results I want, because when I use multiplication and
division, it recognizes the numbers as numbers. But dose anyone know
how to get it to recognizes the numbers in the first place.

Thanks
Kenny

All field values are string values.

Any math operator except the '+' operator should cause conversion to a
numeric type.

parseFloat() and parseInt() can also explicitly convert a string to a
number.

var a * 1 or var a - 0 quickly causes conversion without the overhead of
a function call.

Jul 20 '05 #2
Jerry Park wrote:
var a * 1 or var a - 0 quickly causes conversion without the overhead of
a function call.


You mean a * 1 and a - 0, and +a is quicker :)
PointedEars
Jul 20 '05 #3
Thomas 'PointedEars' Lahn wrote:
Jerry Park wrote:

var a * 1 or var a - 0 quickly causes conversion without the overhead of
a function call.

You mean a * 1 and a - 0, and +a is quicker :)
PointedEars

OK

Jul 20 '05 #4
JRS: In article <3d*************************@posting.google.com> , seen
in news:comp.lang.javascript, Kenny <ke*********@email.com> posted at
Wed, 26 Nov 2003 11:10:04 :-
It treats the values as a string and if the input numbers are 2 and 4
I get 24 and not 6 like I would expect. But dose anyone know
how to get it to recognizes the numbers in the first place.


Yes, anyone who has read the FAQ should know.

One should always seek and read a relevant FAQ before posting to News;
it often gets the answer quicker, and it saves annoyance.

JP's response has been incompletely corrected by PE; see FAQ 4.12.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #5
Dr John Stockton wrote:
Kenny <ke*********@email.com> wrote:
It treats the values as a string and if the input numbers are 2 and 4
I get 24 and not 6 like I would expect.

But dose anyone know
how to get it to recognizes the numbers in the first place.


[...]
JP's response has been incompletely corrected by PE; see FAQ 4.12.


The FAQ entry does not imply in any way that my correction
is wrong as test results posted in other threads have proven.
PointedEars
Jul 20 '05 #6
Dr John Stockton wrote:
Kenny <ke*********@email.com> wrote:
It treats the values as a string and if the input numbers are 2 and
4 I get 24 and not 6 like I would expect.

But dose anyone know how to get it to recognizes the numbers in the
first place.


[...]
JP's response has been incompletely corrected by PE; see FAQ 4.12.


The FAQ entry does not imply in any way that my correction is
wrong (incl. not incomplete) as test results posted in other
threads have proven.
PointedEars
Jul 20 '05 #7
JRS: In article <3F************@PointedEars.de>, seen in
news:comp.lang.javascript, Thomas 'PointedEars' Lahn
<Po*********@web.de> posted at Tue, 2 Dec 2003 16:51:26 :-
Dr John Stockton wrote:
Kenny <ke*********@email.com> wrote:
It treats the values as a string and if the input numbers are 2 and
4 I get 24 and not 6 like I would expect.

But dose anyone know how to get it to recognizes the numbers in the
first place.


[...]
JP's response has been incompletely corrected by PE; see FAQ 4.12.


The FAQ entry does not imply in any way that my correction is
wrong (incl. not incomplete) as test results posted in other
threads have proven.


Yes, it does so imply. You did not correct JP's omission in not stating
that parseInt almost always needs a second parameter of 10. The FAQ
stresses that.

One should recommend reading the FAQ to those who ask rather well-known
questions at every reasonable possibility; doing so may render possible
subsequent questions unnecessary.

If you had looked at FAQ 4.12 yourself, you should have realised that it
deals with only parseInt; and your response ignored parseInt.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #8
Dr John Stockton wrote:
news:comp.lang.javascript, Thomas 'PointedEars' Lahn
Dr John Stockton wrote:
[...]
JP's response has been incompletely corrected by PE; see FAQ 4.12.


The FAQ entry does not imply in any way that my correction is
wrong (incl. not incomplete) as test results posted in other
threads have proven.


Yes, it does so imply. You did not correct JP's omission in not stating
that parseInt almost always needs a second parameter of 10.


In my posting did not refer to his usage of parseInt(...) at all
but only referred to his way of accomplishing conversion without
any function. My correction is therefore not incomplete.
PointedEars
Jul 20 '05 #9

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

Similar topics

10
2103
by: Dave Karmens | last post by:
If I have say 10 fixed variables, how can I set their values = to that of a form that is built dynamically? column1 column2 email = formvalue(0) fname = formvalue(1) lname = formvalue(2)...
3
14908
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
21
3931
by: Michael Bierman | last post by:
Please forgive the simplicy of this question. I have the following code which attempts to determine the color of some text and set other text to match that color. It works fine in Firefox, but does...
0
5483
by: lglmi | last post by:
'********************START OF BAS MODULE******************** Option Explicit Private Type OSVERSIONINFO dwOSVersionInfoSize As Long dwMajorVersion As Long dwMinorVersion As Long...
15
2943
by: sara | last post by:
Hi I'm pretty new to Access here (using Access 2000), and appreciate the help and instruction. I gave myself 2.5 hours to research online and help and try to get this one, and I am not getting...
8
11312
by: Susan Bricker | last post by:
Hi. I am opening a form (frmUserSettings) just before (or so I thought) the Main Menu form (frmMainMenu) is opening but it's not getting the focus. It gets "painted" and then the Main Menu gets...
20
3659
by: MLH | last post by:
120 MyString = "How many copies of each letter do you need?" 150 MyVariant = InputBox(MyString, "How Many?", "3") If MyVariant = "2" Then MsgBox "MyVariant equals the string '2'" If...
1
4900
by: raghuvendra | last post by:
Hi I have a jsp page with 4 columns: namely Category name , Category order, Input field and a submit button. All these are aligned in a row. And Each Category Name has its corresponding Category...
9
2604
Catalyst159
by: Catalyst159 | last post by:
I have a form which is used to calculate residential Floor Area Ratio (FAR). The form is structured into seven parts as follows: Part A: Maximum FAR and Floor Area: Part B: Gross Floor Area of...
0
7242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7418
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...
0
7508
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4737
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...
0
3222
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...
0
1572
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 ...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
446
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.