Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Nov 2004
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    simple question (really!) from newbie

    I have a simple web form with several fields, and want to dynamically calculate the total when the visitor moves from one field to another. I find that when I use the addition operator (+), the fields are concatenated instead of added. (That is to say, values 1 and 2 in the form are showed as 12 instead of 3.) My test with other operators (ie, *) shows that the variables can indeed be treated as numeric values. Is there a different operator I should be using to force values to be added?

    For reference, the script is below and the code of my form is below that.

    Thanks!

    ---------

    function addem2(frm2) { frm2.grandtotal.value = frm2.v01.value + fm2.v02.value }

    ---------

    <form name="frm2" method="get">
    <input type="TEXT" name="v01" value="" size="6" maxlength="5" onChange="addem2(this.form)"><br>
    <input name="v02" type="TEXT" id="v02" onChange="addem2(this.form)" value="" size="6" maxlength="5"><br>
    <input type="TEXT" name="grandtotal" value="" size="6">
    </form>

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,521
    Thanks
    3
    Thanked 507 Times in 494 Posts
    This is the most common newby problem, so you are not alone

    Text box values are strings

    '4' + '4' = '44'

    therefor the string must be converted to a number

    methods

    Number('4') = 4;

    parseInt('4') =4;

    parseInt('4.4')= 4;

    parseFloat('4.4')=4.4;

    Number('4.4')= 4.4;

    Number('4.4')+'4.4' = '4.44.4';

    mutiplying or dividing a valid string number will autotype to a number

    '4.4'*2 = 8.8;

    to check for a valid number

    isNaN('4.4) is false

    isNaN('4.4X') is true

    to replace none digits

    x.value=x.value.replace(/\D/g,''); // result is still a string but is only digit charactors

  • #3
    New to the CF scene
    Join Date
    Nov 2004
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    awesome. thank you very much !!

  • #4
    Regular Coder
    Join Date
    Sep 2005
    Location
    Chicago, IL
    Posts
    160
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    function addem2(frm2) { frm2.grandtotal.value = (frm2.v01.value*1) + (frm2.v02.value*1) }
    If you times each number by 1 you can add them that way too, the same way he said above.

  • #5
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,521
    Thanks
    3
    Thanked 507 Times in 494 Posts
    HE said

    mutiplying or dividing a valid string number will autotype to a number


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •