Custom keystroke script - allow for delete

New Here ,
Mar 04, 2019

Copy link to clipboard

Copied

I've created a text field with format custom keystroke script allowing for only digits. I've used fairly simple regular expression /^\d$/ to accomplish this. However, I would like to have possibility for user to be able to use backspace to delete some of these digits. Now it is not possible for backspace is not allowed keystroke. How do I do that with regular expression? I found that \b should match the backspace but I cannot figure out how to put it in a regular expression definition so it works.

Many thanks,

Vladan

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

When text is being deleted event.change is empty, so you have to allow for that as well.

You can do it by changing this line:

if(re.test(event.change) == false){

To:

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

TOPICS
Acrobat SDK and JavaScript

Views

238

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Custom keystroke script - allow for delete

New Here ,
Mar 04, 2019

Copy link to clipboard

Copied

I've created a text field with format custom keystroke script allowing for only digits. I've used fairly simple regular expression /^\d$/ to accomplish this. However, I would like to have possibility for user to be able to use backspace to delete some of these digits. Now it is not possible for backspace is not allowed keystroke. How do I do that with regular expression? I found that \b should match the backspace but I cannot figure out how to put it in a regular expression definition so it works.

Many thanks,

Vladan

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

When text is being deleted event.change is empty, so you have to allow for that as well.

You can do it by changing this line:

if(re.test(event.change) == false){

To:

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

TOPICS
Acrobat SDK and JavaScript

Views

239

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Mar 04, 2019 0
Most Valuable Participant ,
Mar 04, 2019

Copy link to clipboard

Copied

Post your code, please.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Mar 04, 2019 0
New Here ,
Mar 04, 2019

Copy link to clipboard

Copied

I'm sorry, here's my code:

var re = /^\d$/;

if(event.willCommit == false){

     if(re.test(event.change) == false){

          app.beep();

          event.rc = false;

         }

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Mar 04, 2019 0
Most Valuable Participant ,
Mar 04, 2019

Copy link to clipboard

Copied

When text is being deleted event.change is empty, so you have to allow for that as well.

You can do it by changing this line:

if(re.test(event.change) == false){

To:

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Mar 04, 2019 1
Explorer ,
Mar 04, 2019

Copy link to clipboard

Copied

The easiest way to limit the user by using numbers only within a text field, is to:

  1. access the text field's properties.
  2. Go to the, "Format" tab.
  3. Set format category = "Numbers."

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Mar 04, 2019 0
Most Valuable Participant ,
Mar 04, 2019

Copy link to clipboard

Copied

If you do that it will remove any zeros from the start of the number and will allow the user to enter the minus symbol as well as the decimal separator.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Mar 04, 2019 1
Vladan71 LATEST
New Here ,
Mar 04, 2019

Copy link to clipboard

Copied

This works perfectly. Thanks a lot for really prompt answer. You're awesome!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Mar 04, 2019 0