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

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

New Here ,
May 19, 2016 May 19, 2016

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!

TOPICS
Acrobat SDK and JavaScript
2.6K
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
LEGEND ,
May 19, 2016 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.

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
New Here ,
May 19, 2016 May 19, 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.

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
LEGEND ,
May 20, 2016 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.

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
New Here ,
Jun 01, 2016 Jun 01, 2016

Thanks for your kind reply.

However, it is still not what I'm looking for.

I tried a few ways to edit the code to what I want, but I don't quite understand how your code works

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

"-" is supposed to allow user to indicate a NIL input.

"+" is supposed to allow user to include an overseas mobile number.

Hence, user is supposed to be able to remove any character if possible.

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
LEGEND ,
Jun 01, 2016 Jun 01, 2016
LATEST

The code uses the RegExp, Regular Expression, object to indicate what is required or optional by position in the string. This is common object  used in many computer languages and applications. There are many tutorials and tools on the web  and books that cover this tool and how to use it. The use of this tool requires that one clearly can completely describe all the possible ways the string could be displayed. You may need to write several pieces of code for the keystroke, format, and validation of the data.

It sounds like you will need both the North American phone numbers and international phone number scripts.

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