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

How to Automatically Add Series of Hyphens to Text Field with A Preset Text While Typing?

Participant ,
Jul 28, 2025 Jul 28, 2025

I have a text field with a default value of "NAFID1" and a mouse up action: on blur that works to always keep the preset text in the field and to protect the preset text from deletion.  In other words, if any character of the preset text is deleted, it will reset the field automatically to the preset text.

// Define your preset text
var presetText = "NAFID1";

// Get the current value of the field
var currentValue = event.target.value;

// If the field is empty or doesn't start with the preset text, add it
if(!currentValue.startsWith(presetText)) {
    event.target.value = presetText; 
}

 

I have paired the on blur action with a custom keystroke script that works to automatically add a hyphen after the 6th, 9th and 11th characters.  I also have a limit of 16 characters set for the field.

// Get the current value of the field, removing any existing hyphens
var v = event.value.replace(/-/g, "");

// Check if the input has reached the 6th character
if (v.length > 6) {
    // Add a hyphen after the 6th character
    v = v.substring(0, 6) + "-" + v.substring(6, v.length);
}
// Update the field with the formatted value
event.value = v;


// Check if the input has reached the 9th character
if (v.length > 9) {
    // Add a hyphen after the 9th character
    v = v.substring(0, 9) + "-" + v.substring(9, v.length);
}
// Update the field with the formatted value
event.value = v;


// Check if the input has reached the 11th character
if (v.length > 11) {
    // Add a hyphen after the 11th character
    v = v.substring(0, 11) + "-" + v.substring(11, v.length);
}
// Update the field with the formatted value
event.value = v;

My problem is:

1) If I delete any of the characters of the preset text, it will reset the entire field to the preset text.  It is not an issue if the preset text is the only thing in the field because if you delete anything, it will reset to the preset text.  It becomes an issue when you have filled out the field with any number of other characters up to the 16 limit and you delete any character of the preset text because it will then reset the field to the preset text deleting all the other characters.

 

2) The character limit is 16 but when you are actively typing, it doesn't account for the three (3) hyphens until the end so the user is able to type in 19 characters actually.

 

My questions are:

1) If the user is filling out the field or has filled out the field and accidentally deletes any characters of the preset text, how can you just reset the preset text and preserve the other remaining characters and not reset the entire field to the preset text?

 

2) How can I prevent the user from exceeding the 16 character limit while typing?

TOPICS
Create PDFs , Edit and convert PDFs , JavaScript , Modern Acrobat , PDF forms
1.2K
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
2 ACCEPTED SOLUTIONS
Community Expert ,
Jul 28, 2025 Jul 28, 2025

Remove all of your actions and scripts from the field.  Enter the following custom format script:

if(!event.value){event.value="NAFID1"}

Enter the following custom keystroke script:

AFSpecial_KeystrokeEx('NaFID1-OO-OO-OOOO');

Enter the following validation script:

event.value=event.value.toUpperCase();

View solution in original post

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
Participant ,
Jul 29, 2025 Jul 29, 2025

I entered this into custom format script:

if(!event.value){event.value="NAFID1"}

and this into validation script:

event.value=event.value.toUpperCase();

and this in a custom keystroke script:

AFSpecial_KeystrokeEx('NaFID1-99-A-9999');

 

View solution in original post

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 ,
Jul 28, 2025 Jul 28, 2025

You need to use a Format script for that, so that the actual value of the field isn't changed when the hyphens are added to it.

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
Adobe Employee ,
Jul 28, 2025 Jul 28, 2025

Hi @Mohamad32744384h15w!

 

My understanding of your request.

To achieve dynamic hyphen insertion (e.g., formatting like TEX-XXXX-XXXX while typing), you’d need to use a custom keystroke script in Acrobat JavaScript. However, real-time input masking is limited in PDF forms, so the best-supported approach is:

 

// Custom Keystroke Script

if (event.willCommit) {

    var v = event.value.replace(/-/g, "");

    if (v.length > 3 && v.length <= 7)

        event.value = v.slice(0, 3) + "-" + v.slice(3);

    else if (v.length > 7)

        event.value = v.slice(0, 3) + "-" + v.slice(3, 7) + "-" + v.slice(7, 11);

}

 

Apply this as a custom Keystroke script on your text field (not Format).

This method is the most reliable given the current Acrobat form scripting support. Wait for more inputs from the experts.


Best regards,
Tariq | Adobe Community Team

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 ,
Jul 28, 2025 Jul 28, 2025

Do you need NAFID1 to be included in the field's value, or are you only concerned that it is displayed in the field.  Is it required that field is always completely filled out to the max characters?

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
Participant ,
Jul 28, 2025 Jul 28, 2025

Yes NAFID1 needs to be included in the field's value.  Yes it is required that the field is always completely filled out to the max character

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
Participant ,
Jul 28, 2025 Jul 28, 2025

Just to clarify, max character limit should be 16 (including 3 hyphens) or 13 (excluding 3 hyphens)

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 ,
Jul 28, 2025 Jul 28, 2025

Please type an example of the field completely filled out.

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
Participant ,
Jul 28, 2025 Jul 28, 2025

NAFID1-25-M-0009

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 ,
Jul 28, 2025 Jul 28, 2025

Remove all of your actions and scripts from the field.  Enter the following custom format script:

if(!event.value){event.value="NAFID1"}

Enter the following custom keystroke script:

AFSpecial_KeystrokeEx('NaFID1-OO-OO-OOOO');

Enter the following validation script:

event.value=event.value.toUpperCase();

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
Participant ,
Jul 28, 2025 Jul 28, 2025

I entered this into custom format script:

if(!event.value){event.value="NAFID1"}

and this into validation script

event.value=event.value.toUpperCase();

But this in a custom keystroke script:

AFSpecial_KeystrokeEx('NaFID1-OO-OO-OOOO');
  1. Custom format category keeps disappearing to Special format category.
  2.  Adobe keeps freezing and closing.
  3. It should be 'NAFID1-OO-O-OOOO'.
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 ,
Jul 28, 2025 Jul 28, 2025

Re #1. Due to a bug (or more accurately, a faulty design choice) in Acrobat scripts that contain "AF" will disappear when entered. They should still work, though.

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 ,
Jul 28, 2025 Jul 28, 2025

1.  It disappears because the Arbitrary Mask displays in the properties, but the format script is still there and still works.

2.  Not for me.  Not sure what's going on there.

3.  Change the Keystroke script to AFSpecial_KeystrokeEx('NaFID1-OO-O-OOOO');

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
Participant ,
Jul 29, 2025 Jul 29, 2025

I see now.  I still had the default value set to NAFID1 which was messing everything up.  I deleted it and no issues.

 

One more thing though, is their a way to ensure the 3rd "O" from the left in 'NaFID1-OO-O-OOOO' has to be a letter i.e. NAFID1-25-M-0009 and not a number?

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 ,
Jul 29, 2025 Jul 29, 2025

Change that O to a capital A in the script.

NaFID1-OO-A-OOOO

If you want the others to be numbers only, change the other Os to 9s

NaFID1-99-A-9999

Os accept letters and numbers.

 

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
Participant ,
Jul 29, 2025 Jul 29, 2025

Interesting!  Thank you!

 

Also what is the significance of the ! in 'if(!event.value)'?

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 ,
Jul 29, 2025 Jul 29, 2025

It's a logical operator that means NOT. In this case, it means that true will be returned if the new value is empty.

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
Participant ,
Jul 29, 2025 Jul 29, 2025

So ! is the java equivalent of NOT in excel vba?

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 ,
Jul 29, 2025 Jul 29, 2025

Yes, pretty much.

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 ,
Jul 29, 2025 Jul 29, 2025

Only this is JavaScript, not Java... The two have a similar name but are quite different.

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
Participant ,
Jul 29, 2025 Jul 29, 2025

I entered this into custom format script:

if(!event.value){event.value="NAFID1"}

and this into validation script:

event.value=event.value.toUpperCase();

and this in a custom keystroke script:

AFSpecial_KeystrokeEx('NaFID1-99-A-9999');

 

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 ,
Jul 29, 2025 Jul 29, 2025

Will you please mark my answer correct?

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
Participant ,
Jul 29, 2025 Jul 29, 2025

Yes sir my apologies!

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
Participant ,
Jul 30, 2025 Jul 30, 2025

I just noticed that it won't let me back space (or delete) to edit after I have already typed the full thing out.  The only way to edit after is to highlight the entire field and hit delete and restart from the beginning.  Is there any way around this?

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 ,
Jul 30, 2025 Jul 30, 2025
LATEST

Unfortunately not.  It's a keystroke script.  Once it's completed and accepted you can't modfiy it.

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