We could clean the string up, removing the "$" and "," characters and using the parseFloat function.
We could call the FormCalc Parse function and an example of calling FormCalc from JavaScript is on John Brinkman's blog.
Or we could assign the string to the formattedValue of a hidden DecimalField and then use the rawValue. This has the same advantages of the FormCalc parse function, with all it's locale processing, without the overhead of calling FormCalc from JavaScript.
All we have to do is set the display pattern of the hidden decimal field to suit, in the example that is;
num{$zzz,zzz,zz9.88}|num{$zzzzzzzz9.88}
So our parseCurrency code now looks like;
function parseCurrency(string)
{
var result = 0;
if (string !== null && string !== undefined)
{
HiddenDecimalField.rawValue = null;
HiddenDecimalField.formattedValue = string;
if (!HiddenDecimalField.isNull)
{
result = HiddenDecimalField.rawValue;
}
}
return result;
}
We have set the rawValue to null before assigning the formattedValue because the rawValue will remain unchanged if the value is not valid.
{
var result = 0;
if (string !== null && string !== undefined)
{
HiddenDecimalField.rawValue = null;
HiddenDecimalField.formattedValue = string;
if (!HiddenDecimalField.isNull)
{
result = HiddenDecimalField.rawValue;
}
}
return result;
}
One got cha with using DecimalFields is the JavaScript type returned by the rawValue is normally a number but if you clear the "Limit Trailing Digits" checkbox you get a string rawValue,
In this case change;
result = HiddenDecimalField.rawValue;
to
result = parseFloat(HiddenDecimalField.rawValue);
You can also use this approach to parse a date with a hidden DateTime field.
Download sample form parseCurrency.
No comments:
Post a Comment