You are viewing limited content. For full access, please sign in.

Question

Question

enable commas within the currency fields to work with calculation

asked on February 20, 2017

I am currently having trouble with implementing a JavaScript to enable commas to the currency fields, and performing a calculation with them.  When enabled the commas function, the calculation only calculate after the commas from the currency fields.  Any suggestion is appreciated, thanks.

 

 

$(document).ready(function () {
  $('.table').on('blur', 'input', sumtotal)
   $('#q17').on('blur', 'input', sumtotal)
   $('#q25').on('blur', 'input', sumtotal)
    //product table function
    function sumtotal() {
      	$('.total input').attr('readonly', false);
        var quantity = 0;
      	var unit = 0;
      	var total = 0;
      	var subtotal = 0;
      	var shipping = $('#q25 input').val();
        var gradtotal = 0;
      	      
        $('.table.cf-table-block tbody tr').each(function () {
            quantity =  parseNumber($(this).find('.quantity input').val()); 
            unit = parseNumber($(this).find('.unit input').val());                    
            subtotal = parseNumber(quantity * unit);

            $(this).find('.total input').val(subtotal.toFixed(2));
            total += subtotal;
        });
      grandtotal = parseNumber(shipping)+parseNumber(total)+(total*$('#q17 input').val());
    $('#q26 input').val(grandtotal);
      //added to force a change call. Currently if an alert is placed into the function it fires off but no formatting
    $('#q26 input').change();
    }
  
  	//parse for number value function
    function parseNumber(n) {
        var f = parseFloat(n); //Convert to float number.
        return isNaN(f) ? 0 : f; //treat invalid input as 0;
      
      }
});

$(document).ready ( function(){
$('[id=Field2]').click(function(){
var test = $('select[id=Field20] option:last-child').text()
$('[id=Field1]').val(test);
});});

$(document).ready(function () {
  
  $(".total input, .etotal input, .shipping input").attr("pattern", '^(\\d+|\\d{1,3}(,\\d{3})*)(\\.\\d{2})?$');
  $(".total input, .etotal input, .shipping input").on("change", function () {
    $(this).val($(this).val().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"));

  });
  
});

 

0 0

Answer

SELECTED ANSWER
replied on February 27, 2017

I suggest you change it to a single line field in order to support the thousand delimiter.

0 0

Replies

replied on February 20, 2017

Forms 10.2 has added the support for using thousand delimiter for currency field and the out of box calculation function can do calculation for currency with thousand delimiter without any problem. Following is an example:

The calculation I added for Amount in the table is : =PRODUCT(INDEX(Table.Quantity,ROW()),INDEX(Table.Unit_Price,ROW()))

The calculation I added for Total Amount is : =SUM(Table.Amount)

 

2 0
replied on February 21, 2017

Hi Tommy

If you are not ready to upgrade to 10.2, an easier work around in previous versions of 10 was to use the Numbers fields, where the comma field and decimal place is configured easily and is supported by the Calculations features, and then use JS to Prepend a $ sign to the Field to make it look like a Currency field. Example below (I used moneyclass as a Class for the Currency fields I was targetting)

$(document).ready(function() {

  $('.moneyclass[attrtype="number"] .cf-field').prepend('<span class="margin-end">$</span>');

}); 

 

2 0
replied on February 24, 2017 Show version history

Thanks for the suggestions guys, just a quick follow up.  After appending the commas to the calculation, now I am getting an invalid input for the currency field.  Any suggestions?

 

 

$(document).ready(function () {
  $('.table').on('blur', 'input', sumtotal)
   //$('#q17').on('blur', 'input', sumtotal)
   $('#q19').on('blur', 'input', sumtotal)
    //product table function
    function sumtotal() {
      	$('.total input').attr('readonly', false);
        var quantity = 0;
      	var unit = 0;
      	var total = 0;
      	var subtotal = 0;
      	var shipping = $('#q19 input').val();
        var gradtotal = 0;
      	      
        $('.table.cf-table-block tbody tr').each(function () {
            quantity =  parseNumber($(this).find('.quantity input').val()); 
            unit = parseNumber($(this).find('.unit input').val());                    
            subtotal = parseNumber(quantity * unit);

            $(this).find('.total input').val(subtotal.toFixed(2));
            total += subtotal;
            $(this).find('.total input').change();
          
        });
          
		grandtotal = parseNumber(shipping)+parseNumber(total)//+(total*$('#q17 input').val());
		$('#q18 input').val(grandtotal);		
        $('#q18 input').change();

            
		//console.log("Total " + total);
		//console.log("Shipping " + shipping);  
		//console.log("Grand totol " + grandtotal);
    }
  

  
  	//parse for number value function
    function parseNumber(n) {
      if (typeof n === 'string' || n instanceof String)
      	n = n.replace(/,/g, '');	

        var f = parseFloat(n); //Convert to float number.     
        return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }
  
	//Format numbers with commas
	$(".unit input, .etotal input, .shipping input").attr("pattern", '^(\\d+|\\d{1,3}(,\\d{3})*)(\\.\\d{2})?$');
	$(".unit input, .etotal input, .shipping input, #q18 input, .total input").on("change", function () {
		$(this).val($(this).val().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"));
	});

});

$(document).ready ( function(){
	$('[id=Field2]').click(function(){
	var test = $('select[id=Field20] option:last-child').text()
	$('[id=Field1]').val(test);
  
	});
});

 

0 0
replied on February 26, 2017

Are you still using the native currency field with Forms 10.0? If do, then getting "invalid input" is the expected behavior for currency only accept 0-9 and "." and"-" in that version. 
 

0 0
replied on February 27, 2017

Xiuhong,

 

I am currently working with Forms version 9.2, do you suggest I change the currency field to a number field or a single line field?

0 0
SELECTED ANSWER
replied on February 27, 2017

I suggest you change it to a single line field in order to support the thousand delimiter.

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.