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

Question

Question

How can we format currency fields in Forms to use commas?

asked on May 29, 2015 Show version history

We have a complex form with many currency fields. I have a request to format those fields using commas to make the value easier to enter when entering large numbers. I saw a post where this can be done on Single Line fields but I get an error when trying to set it on Currency fields. There are so many currency fields and lots of Workflow logic wrapped around them that I would prefer to make the change on the existing Currency fields rather than create new fields.

I should also mention that the Form itself has javascript that is calculating many of these Currency fields to produce totals and calculated sub-totals. So I need to be sure that whatever method to format with commas does not affect the javascript calcualtions.

We would like the currency fields to appear as shown in this mockup:

 

 

Thanks in advance,

-Eric R

0 0

Answer

SELECTED ANSWER
replied on June 3, 2015

Here is an example using your sample form:

And here is the Javascript I'm using

$(document).ready(function () {
  
  //set net worth calculation fields to be read-only
  $('.totalassets input').attr('readonly','True');
  $('.totaldebt input').attr('readonly','True');
  $('.networth input').attr('readonly','True');
  
  //initialize currency input fields
  setfields();
  
  //handle adding more items in collections
  $('.cf-collection-append').on('click', setfields);
  
  //handle removing items from collections
  $(document).on('click', sumtotal);
  
  //changes input fields to allow for commas and replaces the input with the comma formatted value
  function setfields() {
    $('.commas input').attr("pattern", '^(\\d+|\\d{1,3}(,\\d{3})*)(\\.\\d{2})?$');
    $('.commas input').on("change", function () {
      $(this).val($(this).val().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"));
    });
    $('.sumasset, .sumdebt').on('blur', 'input', sumtotal);
  }
  
  //calculates total assets and liabilities
  function sumtotal() {
    var sa = 0;
    var sd = 0;
    $('.sumasset input').each(function () {
      sa += parseNumber($(this).val().replace(/,/g,''));
    });
    $('.totalassets input').val(sa.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"));
    $('.totalassets input').trigger('change');
    $('.sumdebt input').each(function () {
      sd += parseNumber($(this).val().replace(/,/g,''));
    });
    $('.totaldebt input').val(sd.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"));
    $('.totaldebt input').trigger('change');
  }
  
  //calculates total net worth
  $('.totalassets input, .totaldebt input').on('change', function() {
    var a = parseNumber($('.totalassets input').val().replace(/,/g,''));
    var d = parseNumber($('.totaldebt input').val().replace(/,/g,''));
    var n = a - d;
    $('.networth input').val(n.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"));
  });
  
  function parseNumber(n) {
    var f = parseFloat(n); //Convert to float number.
    return isNaN(f) ? 0 : f; //treat invalid input as 0;
  }
  
});

The account balance field uses the CSS class, commas sumasset

The stock value field uses the CSS class, commas sumasset

The loan value field uses the CSS class, commas sumdebt

The amount owed field uses the CSS class, commas sumdebt

The total assets field uses the CSS class, commas totalassets

The total liabilities field uses the CSS class, commas totaldebt

The total net worth field uses the CSS class, commas networth

0 0

Replies

replied on May 29, 2015 Show version history

You can try this Javascript. My currency field is using the CSS class, commas.

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

The currency field is just an html input with a specific pattern. The Javascript changes the pattern for that field to allow for commas and then actually changes the value the submitter inputs and replaces it with the comma formatted value.

You should thoroughly test this though to confirm that it doesn't cause any issues with calculations being done in Workflow. Basic checks work fine for me.

Form being filled out:

Form after clicking out of the field and/or during submission:

Workflow Condition Check:

Also, a future release of Forms should have a built in option to display separators in currency fields.

0 0
replied on May 29, 2015

Thank you Alexander.

I added this javascript and while it does insert the commas correctly, it has impacted my other javascript that is calculating other fields and inserting the sums into a sub-total field.

When I enter 25,000 in one field, that is translated to 25 when it inserts that into the sub-total field.

 

Here is the javascript that is causing the issue. I am not sure what I need to change to get it to retain the complete value as entered:

 

 

0 0
replied on May 29, 2015 Show version history

When using parseFloat, you need to remove the commas. You'll also need to change the line where you save the value into the totaldebts field if you want the total to use commas. You can use something like

$(document).ready(function () {
  
  $('.commas input').attr("pattern", '^(\\d+|\\d{1,3}(,\\d{3})*)(\\.\\d{2})?$');
  
  $('.commas input').on("change", function () {
    $(this).val($(this).val().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"));
  });
  
  $('.sumdebt').on('blur', 'input', sumtotal);
  
  function sumtotal() {
    var s = 0;
    $('.sumdebt input').each(function () {
      s += parseNumber($(this).val().replace(/,/g,''));
    });
    $('.totaldebts input').val(s.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"));
  }
  function parseNumber(n) {
    var f = parseFloat(n); //Convert to float number.
    return isNaN(f) ? 0 : f; //treat invalid input as 0;
  }
  
});

When I call the parseNumber function, I'm sending the value with the commas removed. Then after it does the summing, I convert the total value to a string and add the commas back.

0 0
replied on May 29, 2015

Thanks again Alexander... that seems to have worked.

Only thing I noticed now is the commas don't get applied to any currency fields that happen to be in a repeating collection (other than the original field). If I select the link to add a new collection, then the currency field does not have the commas.

 

Also, the read-only sub total fields that I have where I am placing the result of all the calculations does not get the commas applied either. Not sure if that is because they are read only or something else.

Here is a screenshot of that section. You can see that there are no commas.

 

 

Thanks for your assistance. This is very helpful.

-Eric R

0 0
replied on May 29, 2015

Maybe try

$(document).ready(function () {
  
  setfields();
  
  $('.cf-collection-append').on('click', function () {
    setfields();
  });
  
  function setfields() {
    $('.commas input').attr("pattern", '^(\\d+|\\d{1,3}(,\\d{3})*)(\\.\\d{2})?$');
    $('.commas input').on("change", function () {
      $(this).val($(this).val().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"));
    });
    $('.sumdebt').on('blur', 'input', sumtotal);
  }
  
  function sumtotal() {
    var s = 0;
    $('.sumdebt input').each(function () {
      s += parseNumber($(this).val().replace(/,/g,''));
    });
    $('.totaldebts input').val(s.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"));
  }
  function parseNumber(n) {
    var f = parseFloat(n); //Convert to float number.
    return isNaN(f) ? 0 : f; //treat invalid input as 0;
  }
  
});
0 0
replied on June 3, 2015

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

$(document).ready(function () {
  $('.asset').on('blur', 'input', sumtotal);function sumtotal() {var s = 0;
  $('.asset input').each(function () {s += parseNumber($(this).val());
});
  $('.totalassets input').val(s);
}
function parseNumber(n) {
var f = parseFloat(n.replace(/,/g,'')); //Convert to float number.
return isNaN(f) ? 0 : f; //treat invalid input as 0;
}
});

$(document).ready(function () {
  $('.debt').on('blur', 'input', sumtotal);function sumtotal() {var s = 0;
  $('.debt input').each(function () {s += parseNumber($(this).val());
});
  $('.totaldebts input').val(s);
}
function parseNumber(n) {
var f = parseFloat(n.replace(/,/g,'')); //Convert to float number.
return isNaN(f) ? 0 : f; //treat invalid input as 0;
}
});

$(document).ready(function () {
$('.asset input, .debt input').blur(function () {
$('.totalnetworth input').val($('.totalassets input').val() - $('.totaldebts input').val());
  });
});

 

replied on June 3, 2015

Thanks Alexander but when I use the code you provided above, it seems to break it entirely. That might be more me than you code though ;-)

 

I really appreciate your assistance but I am stuck on getting this to work correctly. Let me further state that I am not a developer so my knowledge of java script is quite limited.

I created a test form so I can play around with the code and I have most everything working correctly but the only things I can't get to work correctly is to have commas inserted in any of the fields that are in a repeating collection (other than the initial field) and to have the commas inserted in the total calculations at the bottom. Here is a screen shot of the test for I have set up that shows where the commas are being placed and where they are not. I have also attached the java script that I am currently using. I would appreciate your review and suggestions on what to change in my code to get this working correctly. The form is essentially a net worth calculator (I didn't include all the possible fields for this test form though). It adds up the persons assets and debts and then subtracts debts from assets to come up with a net worth value.

 

 

Here is the code I am using currently:

 

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

$(document).ready(function () {
  $('.asset').on('blur', 'input', sumtotal);function sumtotal() {var s = 0;
  $('.asset input').each(function () {s += parseNumber($(this).val());
});
  $('.totalassets input').val(s);
}
function parseNumber(n) {
var f = parseFloat(n.replace(/,/g,'')); //Convert to float number.
return isNaN(f) ? 0 : f; //treat invalid input as 0;
}
});

$(document).ready(function () {
  $('.debt').on('blur', 'input', sumtotal);function sumtotal() {var s = 0;
  $('.debt input').each(function () {s += parseNumber($(this).val());
});
  $('.totaldebts input').val(s);
}
function parseNumber(n) {
var f = parseFloat(n.replace(/,/g,'')); //Convert to float number.
return isNaN(f) ? 0 : f; //treat invalid input as 0;
}
});

$(document).ready(function () { 
$('.asset input, .debt input').blur(function () {
$('.totalnetworth input').val($('.totalassets input').val() - $('.totaldebts input').val());
  });
});

 

Thanks again.

-Eric R

0 0
SELECTED ANSWER
replied on June 3, 2015

Here is an example using your sample form:

And here is the Javascript I'm using

$(document).ready(function () {
  
  //set net worth calculation fields to be read-only
  $('.totalassets input').attr('readonly','True');
  $('.totaldebt input').attr('readonly','True');
  $('.networth input').attr('readonly','True');
  
  //initialize currency input fields
  setfields();
  
  //handle adding more items in collections
  $('.cf-collection-append').on('click', setfields);
  
  //handle removing items from collections
  $(document).on('click', sumtotal);
  
  //changes input fields to allow for commas and replaces the input with the comma formatted value
  function setfields() {
    $('.commas input').attr("pattern", '^(\\d+|\\d{1,3}(,\\d{3})*)(\\.\\d{2})?$');
    $('.commas input').on("change", function () {
      $(this).val($(this).val().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"));
    });
    $('.sumasset, .sumdebt').on('blur', 'input', sumtotal);
  }
  
  //calculates total assets and liabilities
  function sumtotal() {
    var sa = 0;
    var sd = 0;
    $('.sumasset input').each(function () {
      sa += parseNumber($(this).val().replace(/,/g,''));
    });
    $('.totalassets input').val(sa.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"));
    $('.totalassets input').trigger('change');
    $('.sumdebt input').each(function () {
      sd += parseNumber($(this).val().replace(/,/g,''));
    });
    $('.totaldebt input').val(sd.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"));
    $('.totaldebt input').trigger('change');
  }
  
  //calculates total net worth
  $('.totalassets input, .totaldebt input').on('change', function() {
    var a = parseNumber($('.totalassets input').val().replace(/,/g,''));
    var d = parseNumber($('.totaldebt input').val().replace(/,/g,''));
    var n = a - d;
    $('.networth input').val(n.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"));
  });
  
  function parseNumber(n) {
    var f = parseFloat(n); //Convert to float number.
    return isNaN(f) ? 0 : f; //treat invalid input as 0;
  }
  
});

The account balance field uses the CSS class, commas sumasset

The stock value field uses the CSS class, commas sumasset

The loan value field uses the CSS class, commas sumdebt

The amount owed field uses the CSS class, commas sumdebt

The total assets field uses the CSS class, commas totalassets

The total liabilities field uses the CSS class, commas totaldebt

The total net worth field uses the CSS class, commas networth

0 0
replied on June 10, 2015

Thank you Alexander! Your code worked perfectly!!!

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

Sign in to reply to this post.