Define Javascript function in Form with hyperref

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!

You can simply cast the values to a number. If the user enters a string (letters) this will result in an "NaN" output, but for the user it is obvious that there went something wrong.

\documentclass[a4paper,10pt]{article}

\usepackage{hyperref}

\begin{document}
\begin{Form}
\TextField[width=.9\textwidth, name=a20]{~}\\
\TextField[width=.9\textwidth, name=a21]{~}\\
\TextField[width=.9\textwidth, name=a22]{~}\\
\TextField[width=.9\textwidth, name=a23]{~}\\
\fbox{
\TextField[width=.9\textwidth, name=sum,
calculate = {
this.getField('sum').value =
(Number(this.getField('a20').value.replace(',', '.')) +
Number(this.getField('a21').value.replace(',', '.')) +
Number(this.getField('a22').value.replace(',', '.')) +
Number(this.getField('a23').value.replace(',', '.'))).toString().replace('.', ',');
}
]{~}}
\end{Form}
\end{document}


Edit: As I suspect you want to display currencies, you might want to use .toFixed(2) on the sum.

\documentclass[a4paper,10pt]{article}

\usepackage{hyperref}

\begin{document}
\begin{Form}
\TextField[width=.9\textwidth, name=a20]{~}\\
\TextField[width=.9\textwidth, name=a21]{~}\\
\TextField[width=.9\textwidth, name=a22]{~}\\
\TextField[width=.9\textwidth, name=a23]{~}\\
\fbox{
\TextField[width=.9\textwidth, name=sum,
calculate = {
this.getField('sum').value =
(Number(this.getField('a20').value.replace(',', '.')) +
Number(this.getField('a21').value.replace(',', '.')) +
Number(this.getField('a22').value.replace(',', '.')) +
Number(this.getField('a23').value.replace(',', '.'))).toFixed(2).toString().replace('.', ',');
}
]{~}}
\end{Form}
\end{document}

• Nice, thx! But now Integers doesn't work anymore. :/ – Cauti Dec 5 '17 at 10:11
• @Cauti For me they work flawlessly. What do you mean with integers? My example works with something like "1" in the first field and "5" in the third field. – TeXnician Dec 5 '17 at 10:39
• Didn't work for me. When I iput "1" it is ignored in the sum. Copy-pasted the code above. – Cauti Dec 5 '17 at 12:22
• @Cauti I can't reproduce this error. With an up-to-date TeXLive and Okular (under Linux) it works. You could try putting a parseFloat() around the additions and then use the .toFixed() on the result. – TeXnician Dec 5 '17 at 14:36