#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    20
    Rep Power
    0

    Jquery NaN when value > 1000


    Code:
    (function($) {
    $().ready(function() {
    	var sum = 0;
    	var b = 0;
    	var p = 0;
    	var t = 0;
    	var bal = $('#edit-pay-balance-2').val();		
         $('#edit-pay-amount-2').keyup(function () {
    
    		p = $('#edit-pay-amount-2').val();
    
    		var new_sum = (p - t);
    		sum = new_sum.toFixed(8);
    		$('#edit-pay-total-total-2').val(sum);
    		
    		var new_bal = (bal - new_sum);
    		var adj_bal = new_bal.toFixed(8);		
    		$('#edit-pay-balance-2').val(adj_bal);
    		
         });
    });
    })(jQuery);
    when the adj_bal is > 1000, the $('#edit-pay-balance-2').val(adj_bal); field displays: NaN because of the commas in the value.
    I've tried adj_bal.val().replace(/,/g, '') and new_bal.val(replace(/,/g, '')) and a number of variants, but an not getting the right combo.
    Help appreciated.
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,173
    Rep Power
    184
    Why don't you just prevent the text input field(s) from receiving commas? Just limit the text input field(s) to numbers only... use a Regular Expression to do this.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    20
    Rep Power
    0
    Originally Posted by web_loone08
    Why don't you just prevent the text input field(s) from receiving commas? Just limit the text input field(s) to numbers only... use a Regular Expression to do this.
    The user doesn't get to input the number. It is pulled from the database, but the php default puts a comma in it. I am using jquery on a div, so I need to format the number after the calculation is done.
  6. #4
  7. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,173
    Rep Power
    184
    If PHP is formatting the number; just remove number_format() from your echo/print or... if the number has been formatted and then put in the database; just do a preg_replace() and strip out the commas. Yes, formatting the number, after the configuration occurs, would be your best bet.

IMN logo majestic logo threadwatch logo seochat tools logo