Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

event.willCommit, JavaScript, Acrobar, event.rc

Community Beginner ,
Oct 26, 2022 Oct 26, 2022

hello, I have a simple script that does not allow to insert anything but numbers. Unfortunately, I guess I don't understand how the willCommit event works. Until now, I thought that if(!event.willCommit) would rerun the script every time the field changed before committing the value. And event.willCommit (without the exclamation point) will run the script when the event is committed. However, the following script makes it impossible to fill the field at all (neither with numbers nor with any characters). What am I doing wrong? And could someone really explain to me how willCommit actually works?

 

var ff = /[0-9]+/g;


if(!event.willCommit){

event.rc = ff.test(event.value);

}

TOPICS
JavaScript , PDF forms
1.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 26, 2022 Oct 26, 2022

If you want to allow only numbers, you don't need keystroke script, you can format field as number.

Field formatted as number will only allow for numbers and decimal point.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 26, 2022 Oct 26, 2022

The keystroke (or paste) value entered into the field is "event.change". The "event.value" contains the existing data in the field.  Also, the RegEx pattern needs to restrict the test to entire entered data. Otherwise someone could paste "aa123bb" into the field and it would pass the test. 

The script should be:

 

 

var ff = /^[0-9]+$/;

if(!event.willCommit){
   event.rc = ff.test(event.change);
}

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 26, 2022 Oct 26, 2022
LATEST

It's all described in the API Reference. Here's the description of the willCommit propery:

 

Verifies the current keystroke event before the data is committed. It can be used to check target form field
values to verify, for example, whether character data was entered instead of numeric data. JavaScript sets
this property to true after the last keystroke event and before the field is validated.

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