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

Question

Question

Format a currency field

asked on November 12, 2015

How can i format a currency field in a form to accept 4 decimal places?  We have a form for purchase orders and some of the items we purchase have a cost that is 4 decimal places long.  Right now we have this field set as US dollars and it will only accept 2 decimal places.

 

0 0

Answer

SELECTED ANSWER
replied on November 12, 2015

Give your currency field a CSS class name like fourdecimal. Then you can use JavaScript like

$(document).ready(function () {
  $('.fourdecimal input').attr("pattern", '^(?:-?(?:0|[1-9][0-9]*)(?:\\.[0-9]{0,4})?)?$');
});
2 0

Replies

replied on November 12, 2015 Show version history

Try this java script for your form:

Just make sure you the change word "changemetoyourcurrencyclass" to the field class name

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

 

Where to update the class name:

1 0
replied on November 12, 2015

Thank you...However, I wasn't clear enough...I need 4 digits behind the decimal.

 

 

I need to accept entries like.....

1.00

12.34

123.123

1234.1234

1.1111

Basically any amount that has 1-4 digits behind the decimal. 

 

0 0
replied on November 12, 2015

Thank you...However, I wasn't clear enough...I need 4 digits behind the decimal.

 

0 0
replied on November 12, 2015

I need to accept entries like.....

1.00

12.34

123.123

1234.1234

1.1111

Basically any amount that has 1-4 digits behind the decimal. 

0 0
SELECTED ANSWER
replied on November 12, 2015

Give your currency field a CSS class name like fourdecimal. Then you can use JavaScript like

$(document).ready(function () {
  $('.fourdecimal input').attr("pattern", '^(?:-?(?:0|[1-9][0-9]*)(?:\\.[0-9]{0,4})?)?$');
});
2 0
You are not allowed to follow up in this post.

Sign in to reply to this post.