- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2019 11:40 AM
How can you enforce currency formatting on a specific field on a catalog item, without a dependency on another field?
That is, user enters 10000, I need the field they entered the value into to display $10,000.
The OnChange client script below on my salaryYear3 single text string field appears to do nothing, which is probably better than an infinite loop, but not the outcome I need. FWIW - the currencyFormat(num) function has been tested and works fine for either numbers or string numbers.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading ) {
return;
}
g_form.setValue('salaryYear3', currencyFormat(newValue));
}
function currencyFormat (num) {
// Input: Number or string
// Output: currency format; negatives as ($100,123), not -$100,123 (client script on field sets format to red)
// Return US Currency notation for valid int and float numbers only, run isGood(x) first.
if (typeof(num) === 'string') {
clnNum = num.replace(/\$|,|(|)/g,'');
} else {
clnNum = num.toString();
clnNum = clnNum.replace(/\$|,|(|)/g,'');
}
var mNum = Number(clnNum);
if ( mNum >= 0 ) {
return "$" + mNum.toFixed(0).replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
} else {
var absNum=Math.abs(mNum);
var negAmount = "$" + absNum.toFixed(0).replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
return "(" + negAmount + ")";
}
}
I would like to be able to do with same with decimal numbers. That is, if user enters 12345.4512, would like the field to display 12,345.5, for example.
Thank you in advance.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-14-2019 10:00 AM
OMG - I can't believe this post has gone this long without serious input. Thus, I am going to have to assume the solution is just excessively, blatantly obvious to everyone but myself. So for any future novices out there... based on the brilliant thread by "b.s." here.
Basically, in an OnChange script,
oldValue = the value when the form loads
newValue = the current value
and,
var previousValue = g_form.getValue('yourfieldnamehere'); = the prior value of the field
Thus, this OnChange client script performs the functionality I was after.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading ) {
return;
}
var previousValue = g_form.getValue('salaryYear3');
if ( currencyFormat(newValue) === previousValue ) {
return;
} else {
g_form.setValue('salaryYear3', currencyFormat(newValue));
}
Where currencyFormat(x) from my OnLoad client script is:
function currencyFormat (num) {
// Input: Number or string
// Output: currency format; negatives as ($100,123), not -$100,123
// Return US Currency notation for valid int and float numbers
// ================================================
if (typeof(num) === 'string') {
clnNum = num.replace(/\$|,|(|)/g,'');
} else {
clnNum = num.toString();
clnNum = clnNum.replace(/\$|,|(|)/g,'');
}
var mNum = Number(clnNum);
if ( mNum >= 0 ) {
return "$" + mNum.toFixed(0).replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
} else {
var absNum=Math.abs(mNum);
var negAmount = "$" + absNum.toFixed(0).replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
return "(" + negAmount + ")";
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2019 12:37 PM
Try:
var num = 12345.4512;
var n = num.toFixed(2);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2019 12:47 PM
Thank you for the quick reply, my question is not so much about how to format a number per se. The question, phrased another way, is how to self-format the value entered into a specific catalog field by a user. That is, how can this be done on the platform, since an OnChange client script that runs on itself would, I think, create an infinite loop.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-14-2019 10:00 AM
OMG - I can't believe this post has gone this long without serious input. Thus, I am going to have to assume the solution is just excessively, blatantly obvious to everyone but myself. So for any future novices out there... based on the brilliant thread by "b.s." here.
Basically, in an OnChange script,
oldValue = the value when the form loads
newValue = the current value
and,
var previousValue = g_form.getValue('yourfieldnamehere'); = the prior value of the field
Thus, this OnChange client script performs the functionality I was after.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading ) {
return;
}
var previousValue = g_form.getValue('salaryYear3');
if ( currencyFormat(newValue) === previousValue ) {
return;
} else {
g_form.setValue('salaryYear3', currencyFormat(newValue));
}
Where currencyFormat(x) from my OnLoad client script is:
function currencyFormat (num) {
// Input: Number or string
// Output: currency format; negatives as ($100,123), not -$100,123
// Return US Currency notation for valid int and float numbers
// ================================================
if (typeof(num) === 'string') {
clnNum = num.replace(/\$|,|(|)/g,'');
} else {
clnNum = num.toString();
clnNum = clnNum.replace(/\$|,|(|)/g,'');
}
var mNum = Number(clnNum);
if ( mNum >= 0 ) {
return "$" + mNum.toFixed(0).replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
} else {
var absNum=Math.abs(mNum);
var negAmount = "$" + absNum.toFixed(0).replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
return "(" + negAmount + ")";
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-02-2019 06:18 AM
Thank You for the posting. this is what I have needed. Going to save this off.