Skip to main content
Participant
March 4, 2019
Answered

Custom keystroke script - allow for delete

  • March 4, 2019
  • 2 replies
  • 1479 views

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

This topic has been closed for replies.
Correct answer try67

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){

2 replies

logistics227043683
Known Participant
March 4, 2019

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."
try67
Community Expert
Community Expert
March 4, 2019

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.

Vladan71Author
Participant
March 4, 2019

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

try67
Community Expert
Community Expert
March 4, 2019

Post your code, please.

Vladan71Author
Participant
March 4, 2019

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;

         }

}

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
March 4, 2019

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){