I have TextFields with the environment Form from hyperref package. However I want to add the values of the TextFields and print the sum in another TextField. That works well with the following code:
\begin{Form}\fbox{
\TextField[width=.9\textwidth, name=sum,
calculate = {
this.getField('sum').value =
this.getField('a20').value +
this.getField('a21').value +
this.getField('a22').value +
this.getField('a23').value;
}
]{~}}
\end{Form}
My problem is, that if e.g. a20 is empty, the + don't add the value 0 but concatenates all following values like Strings. Furthermore I want to convert Inputs like 42,50 to the form 42.50 to calculate them like floats.
So I tried to define a Javascript function like this, but it doesn't work:
\begin{Form}\fbox{
\TextField[width=.9\textwidth, name=sum,
calculate = {
this.getField('sum').value =
isNumber(this.getField('a20').value) +
this.getField('a21').value +
this.getField('a22').value +
this.getField('a23').value +
this.getField('a24').value;
},
function isNumber(n) {
n = n.replace(/\./g, '').replace(',', '.');
return !isNaN(parseFloat(n)) && isFinite(n);
}
]{~}}
\end{Form}
The error is, that the function is obviously at the wrong place. Is there a way to make it work?
Thanks!