Click to See Complete Forum and Search --> : help with numerical textboxes


cowasoo
04-20-2005, 10:28 PM
i want to collect a numerical value from two text boxes and add them together but i when i try i always get 1+1=11 instead of 1+1=2 how do u define it as numerical and not a string

phpnovice
04-20-2005, 10:32 PM
Form fields always contain string values. Thus, you must convert the content to numbers for calculation purposes. For example:

var nbr1 = Number(document.formName.fieldName1.value);
If (isNaN(nbr1)) nbr1 = 0;
var nbr2 = Number(document.formName.fieldName2.value);
If (isNaN(nbr2)) nbr2 = 0;
document.formName.fieldName3.value = (nbr1 + nbr2).toFixed(2);

balloonbuffoon
04-20-2005, 10:36 PM
This probably isn't the most conventional way but you could subtract the opposite. (Strings can't be subtracted like they can be joined by adding.) ie. 1-(-1)=2 It seems to work for me. :)

-Steve

Charles
04-21-2005, 04:32 AM
var nbr1 = Number(document.formName.fieldName1.value);
If (isNaN(nbr1)) nbr1 = 0;
var nbr2 = Number(document.formName.fieldName2.value);
If (isNaN(nbr2)) nbr2 = 0;
document.formName.fieldName3.value = (nbr1 + nbr2).toFixed(2);
We can shorten that to:
var nbr1 = Number(document.formName.fieldName1.value) || 0;
var nbr2 = Number(document.formName.fieldName2.value) || 0;
document.formName.fieldName3.value = (nbr1 + nbr2).toFixed(2);

phpnovice
04-21-2005, 07:12 AM
It isn't always about the shortest way (even the above could be shorter and more efficient -- by eliminating redundant object references). In this forum, there is also something to be said for code which is more instructional. That is what I like to post -- unless the OP is specifically looking for shorter, more efficient code.

Juuitchan
04-21-2005, 12:53 PM
Will that really work, Charles?

nusense
04-21-2005, 01:00 PM
i want to collect a numerical value from two text boxes and add them together but i when i try i always get 1+1=11 instead of 1+1=2 how do u define it as numerical and not a string

Thats because they are numerical strings, if you take the content of the box and return it to a variable and subtract zero from it, the result would be numeric.

1+1=11 is correct for strings as your adding one element to the other...

if you said...

(1-0)+(1-0)= ?? Try it and see.

no need for this parsInt stuff if you know the input to be anumerical value or you can limit the input to numerical values...

Charles
04-21-2005, 02:47 PM
Will that really work, Charles?Try <script type="text/javascript">
<!--
alert (NaN || 0)
// -->
</script>

Charles
04-21-2005, 02:48 PM
It isn't always about the shortest way (even the above could be shorter and more efficient -- by eliminating redundant object references). In this forum, there is also something to be said for code which is more instructional. That is what I like to post -- unless the OP is specifically looking for shorter, more efficient code.I wasn't being critical, I was just adding something that I thought might be of interest.

phpnovice
04-21-2005, 03:54 PM
I was just adding something that I thought might be of interest.
It was interesting -- but an explanation of why it works would have been even more interesting, plus instructional to boot. ;)

Charles
04-21-2005, 04:00 PM
It was interesting -- but an explanation of why it works would have been even more interesting, plus instructional to boot. ;)Except that I'm not entirely sure why it works. I know why it works in Perl but I'd have to do some reading up on ECMA spec to be able to speak to why it works in JavaScript.