Skip to main content
Participant
Mar 4, 2019 10:11 PM UTC
Answered

Custom keystroke script - allow for delete

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 is closed to new replies. Start a new post to keep the conversation going.
try67
try67Correct answer
Community Expert
Mar 4, 2019 11:03 PM UTC

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

6 replies

logistics227043683
Known Participant
Mar 4, 2019 10:51 PM UTC

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."
Translate
try67
Community Expert
Community Expert
Mar 4, 2019 11:04 PM UTC

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.

Translate
Vladan71Author
Participant
Mar 4, 2019 11:16 PM UTC

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

Translate
try67
Community Expert
Community Expert
Mar 4, 2019 10:47 PM UTC

Post your code, please.

Translate
Vladan71Author
Participant
Mar 4, 2019 10:55 PM UTC

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;

         }

}

Translate
try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
Mar 4, 2019 11:03 PM UTC

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

Translate