1

I would like to have a TextField that contains only numbers in order to make a date like [TextField]/[TextField]/[TextField]. I also need to restrict the length, but it was asked before.

Comparing two different outputs

MWE:

\documentclass{article}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{hyperref}

\begin{document}

\begin{Form}
    \TextField[width=1em,charsize=7pt,maxlen=2]{Text}
\end{Form}

\end{document}

I am using Acrobat Reader DC and it is preferable that the answer to this question (which seems to be unlikely to be answered) has the following requirements:

  1. It should work on most possible viewers.
  2. If the input contains text or numbers mixed with text or any other character that is not number then the code should denied the input, that is, we should not be able to see how an "a" or "b1" or "bb" is written in our viewer but "11", "99", "01" etc.

Thanks!!

  • 1
    Probably not without javascript and then you severely cut down on the number of supported viewers. – daleif Mar 7 '19 at 9:29
  • @daleif thank you! Uh, I thought there would be no problems with the viewers :(. – manooooh Mar 7 '19 at 9:31
3

A simple version which outputs NaN (not a number) if it is not a number. You can simply include some more JavaScript to check if it is NaN and then empty the field if that is what you want.

Please note: JavaScript is always a bit viewer-dependent. I have only tested with Okular.

\documentclass{article}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{hyperref}

\begin{document}

\begin{Form}
    \TextField[width=1em,charsize=7pt,maxlen=2,name=text,calculate={this.getField('text').value=Number(this.getField('text').value)}]{Text}
\end{Form}

\end{document}
  • Uhm, I would like to have a solution that covers almost any viewer's features, but this seems a good solution! 1) I compiled your code and I do not know how to produce NaN (I am using Acrobat Reader DC). 2) Is it possible that only any text char is disabled without the appearance of windows of any kind? That is, if the user writes a number then it is written, otherwise nothing is written/showed. – manooooh Mar 7 '19 at 9:38
  • 1
    @manooooh You can produce "NaN" by inputting not a number, e.g. input "1a" and you will get "NaN" in the text field (you need to get it to calculate so you might want to focus something else first). Concerning your second point (if I understand that correctly, because I have no idea which kind of window you refer to) you would have to intercept user input which seems like a much heavier and even more viewer-dependent task… – TeXnician Mar 7 '19 at 9:43
  • Concerning to the "window" I mean that no pop-up window should appear (if there were any by default). Concerning of the way that the restriction should work, well, I did not think it could become complicated and even more restrictive, but I believe you. P.S. I have been able to achieve the NaN effect but maybe I am very clumsy that I have to click several times so that the input is updated to the NaN state, which is a bit annoying. – manooooh Mar 7 '19 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.