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
    Regular Coder
    Join Date
    Mar 2006
    Posts
    200
    Thanks
    27
    Thanked 2 Times in 2 Posts

    adding up text boxes

    Hello,

    I am trying to add up the values of dynamically created textboxes.

    I'm using php to create the text boxes depending on the options a user selects. Each box is called number.

    I am trying to use JavaScript to add up the values of these boxes. It's working on natural numbers, but not on decimals.

    Any idea why? It just says NaN which I assume is JS saying I can't do it!

    Thanks,

    Nicky

    Code:
    <script language=javascript>
    function add() {
    var sum = 0;
    var valid = true;
    var inputs = document.getElementsByName( 'number');
    for(i =0; i < inputs.length; i++) {
    if( inputs[i].value.match( /(^(0|[1-9][0-9]*)$)|((^(0?|[1-9][0-9]*)\.(0*[1-9][0-9]*)$)|(^[1-9]+[0-9]*\.0+$)|(^0\.0+$))/ )) {
    sum+=parseFloat(RegExp.$1);
    }
    else {valid=false;}
    }
    if(valid) {
    document.getElementById( 'sum').value = sum;
    }
    else{
    alert("Please enter numbers only");
    }
    }
    </script>

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    what do you mean by parseFloat(RegExp.$1)?

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    200
    Thanks
    27
    Thanked 2 Times in 2 Posts
    That was my attempt to add up decimal numbers. I followed a tutorial online, and it had parseInt there. I changed it to parseFloat to try to get it to understand the decimal place.

    I know little about Javascript, so if you have another way of doing this I am all ears. Or point me to a tutorial...

    Thanks

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Code:
    <script type = "text/javascript">
    function add() {
    var sumtot = 0;
    var valid = true;
    var inputs = document.getElementsByName('number');
    for(i = 0; i < inputs.length; i++) {
    if (isNaN(inputs[i]) {
    alert ("Please enter numbers only");
    valid = false;
    return valid;
    }
    sumtot = sumtot + parseFloat(inputs[i]);
    }
    if (valid) {
    document.getElementById('sum').value = sumtot;
    }
    }
    </script>

    Note: change variable sum to sumtot.
    It is never a good idea to use variable or function name which is the same as an element name or ID. You should also avoid giving names or id's to your variables/functions/arguments/forms words which are JavaScript methods/attributes such as 'name' or 'id' or 'value' or 'checked' or 'submit' or 'replace' or 'parseInt'.



    We have been asked to correct our report on the wedding last week. On return from honeymoon the happy couple will be staying at The Old Manse and not as we stated at the home of the bridegroom's father.

  • Users who have thanked Philip M for this post:

    nickyfraggle (09-07-2008)

  • #5
    Regular Coder
    Join Date
    Mar 2006
    Posts
    200
    Thanks
    27
    Thanked 2 Times in 2 Posts
    Thank you Phil. That's really appreciated.

    Nicky


  •  

    Posting Permissions

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