Skip to main content
Participating Frequently
May 26, 2022
Answered

How to use only Digits ,Backspace ,Delete button only in PDF using JavaScript

  • May 26, 2022
  • 3 replies
  • 2454 views

How to modify the following code just to add backspace and delete button to the allowed list.

 

var digits = /^\d$/;

if(event.willCommit == false){

  if(event.change && digits.test(event.change) == false){

          app.beep();

          event.rc = false;

         }

}

 

This topic has been closed for replies.
Correct answer try67

@try67  I have tried that but the problem is dealing with 500-800 list items (500-800 text box) and its formula's make the report too slow to load.

Multi line text box gives me the result I want except the little issue everything is ok with the use of match function , .map function , join function..

anyway thanks for your support on the first question.


Try this, then, as the custom Keystroke script:

 

if (AFMergeChange(event).split("\r").length!=5) {
event.rc = false;
}

 

This will allow the user to only have 5 lines in the field. Of course, you have to make sure there are already that many lines when they get it, or they won't be able to edit it at all. Also, after you apply the code it's likely it will "disappear". This is a bug of Acrobat, but it should still work, despite not being visible.

3 replies

try67
Community Expert
Community Expert
May 27, 2022

PS. Please do not post the same question multiple times in different forums.

Sibi MGAuthor
Participating Frequently
May 30, 2022

@try67  Thanks for the reply and sorry for the repeated questions , as I am not sure in the begining where to post correctly.

Is there any  to delete the repeated ones?

 

I tried with the code you have suggested and it is working perfectly fine.

I have another situation here.

Situation:

I have changed the textbox to multiline option so that I  can multiply with another multiline textbox to get results (To avoid creating 100's of textboxes i am doing this).

                                           var digits = /^\d*$/;

Qn 1 :How can I modify the code to accept  enter key also to the code to work with multiline text box?

Qn 2 : I have noticed that with the multiline txt box if  I press BakSpce/Del it will clear the data and if the user press the buttons again it is also deleting the line.Which is messing my data....Can you recommend a solution to avoid this?

Requirements in the below picture :

 

 

 Thanks

 

 

try67
Community Expert
Community Expert
May 30, 2022

1. Change it to:

var digits = /^[\d\n]*$/;

2. I don't understand. You want to prevent the user from deleting empty lines?

bebarth
Community Expert
Community Expert
May 26, 2022

Hi,

Correct, this script works fine!
As you test event.change, you don't need  the start and end symbols in the regular expression. You can just write:

 

var digits = /\d/;

 

@+

try67
Community Expert
Community Expert
May 27, 2022

Actually, if you're going to change it I would change it to this:

var digits = /^\d*$/;

Doing it your way, if someone pasted the text "a12b" into the field the script you provided will accept it, which you don't want. With my change that won't be the case, but they could paste "1234". With the original code they could only paste single digits at a time. Also, sometimes when you type very quickly the value of the change property is more than one character, so it's good to keep the code flexible, but not too much...

bebarth
Community Expert
Community Expert
May 27, 2022

I knew I was tired last night! Usually I think about the pasting, but not this time.
On the other hand, I didn't know about the speed of typing... because that can't happen to me in accordance with my typing level. 😉
Sorry to have obliged you to correct an answer.

try67
Community Expert
Community Expert
May 26, 2022

It does already. Works fine for me.