Skip to main content
Participant
May 19, 2016
質問

How to only allow numbers, "+" and "-" for a textfield that is meant for phone number?

  • May 19, 2016
  • 返信数 1.
  • 2719 ビュー

Hi guys,

I am also unsure if I should be inserting the code on Format or Validate.

If there's any tutorial on the JavaScript language for Adobe Acrobat, kindly leave links down below because I would like to learn more on this.

Thanks in advance for anyone that will be kind to help!

このトピックへの返信は締め切られました。

返信数 1

Inspiring
May 19, 2016

Adobe only supplies a predefined format for North American phone numbers entered as a 10 digit number, "[(]999[)][--. ]999[-. ]9999". This is all done using the RegExp object. The tutorials start with the W3 schools and other web sites The RegExp is common to many languages besides JavaScript. If you write your own validation script you may also need to write you own custom keystroke and custom format scripts.

tjingyuan作成者
Participant
May 20, 2016

What if I want to just have the textfield as a Custom format since I'm not from North American?

I was using this code previously that I found which worked well since I only needed to have the textfield in numbers:

// Document-level function

function triDigKS() {

    // Get all that is currently in the field

    var val = AFMergeChange(event);

    // Reject entry if anything but digits

    event.rc = AFExactMatch(/\d{0,10}/, val);

}

// Custom Keystroke script

triDigKS();

---------

However, I was required to allow additional "-" and "+" characters on top of numerics, but I have no idea how.

Inspiring
May 20, 2016

Why not just add the RegExp for  the "+" character?

// Document-level function

function triDigKS() {

    // Get all that is currently in the field

    var val = AFMergeChange(event);

    // Reject entry if anything but "+" with digits or a null string;

    event.rc = AFExactMatch(/^[+]\d{0,10}$/, val));

    return;

}

I am not sure where you want the "-" separators to appear.

This code does not allow for the manual removing of the plus sign and number or optionally not entering the "+" as part of the number. That is easily fixed by adding additional RegExp strings  to the array for the AFExactMatch function.

When one writes code one needs to have a clear and full statement of the requirements.