Skip to main content
Participant
August 22, 2025
Answered

Automatically format a date with dashes during typing

  • August 22, 2025
  • 2 replies
  • 267 views

Hi -  I am working on editing a form that has many date fields that the user needs to enter. I want the user to be able to type digits continuously (e.g., 08222025), and as they type a slash ("/") is automatically inserted after the 2nd and 4th digits. So the final format appears as: MM/DD/YYYY (e.g., 08/22/2025). The formatting should happen live as the user types, not after they exit the field. Ideally this would limit input to only digits and input length to 8 digits. 

 

I have been trying various versions of JavaScript in the Custom Keystroke Script field but have not had any luck. I am a novice at JavaScript especiallly within the Adobe environment so any help is appreciated!

 

Correct answer Tariq Dar

Hi @angelica_5902,

 

That’s a great question — thank you for reaching out! 
@PDF Automation Station has pointed you in the right direction.

If you want to take the JavaScript route.

 

Here’s a sample script you can try in the Custom Keystroke Script:

if (!event.willCommit) {
    // Only allow digits
    event.change = event.change.replace(/\D/g, "");
    var v = event.value + event.change;

    // Limit to 8 digits
    v = v.substring(0, 8);

    // Insert slashes automatically
    if (v.length > 2) v = v.substring(0,2) + "/" + v.substring(2);
    if (v.length > 5) v = v.substring(0,5) + "/" + v.substring(5);

    event.rc = true;
    event.value = v;
}

 

A couple of tips:

  • Place this code in the Custom Keystroke Script for the date field (Properties > Format > Custom).

  • You may still want to add a validation script to check that the final date is valid (for example, not 02/30/2025).

  • Acrobat JavaScript runs in a sandboxed way, so testing step by step (typing slowly) helps you debug.

 

⚠️ Disclaimer: Adobe does not provide official support for custom JavaScript in PDFs. The script above is a community-suggested approach and represents the best possible support we can provide here in the forums.

 

Since you mentioned you’re a novice with Acrobat JavaScript, don’t worry. Experimenting with these small snippets is the best way to learn!

 

Best regards,
Tariq | Adobe Community Team | Meet Acrobat Studio

2 replies

Tariq DarCorrect answer
Legend
August 22, 2025

Hi @angelica_5902,

 

That’s a great question — thank you for reaching out! 
@PDF Automation Station has pointed you in the right direction.

If you want to take the JavaScript route.

 

Here’s a sample script you can try in the Custom Keystroke Script:

if (!event.willCommit) {
    // Only allow digits
    event.change = event.change.replace(/\D/g, "");
    var v = event.value + event.change;

    // Limit to 8 digits
    v = v.substring(0, 8);

    // Insert slashes automatically
    if (v.length > 2) v = v.substring(0,2) + "/" + v.substring(2);
    if (v.length > 5) v = v.substring(0,5) + "/" + v.substring(5);

    event.rc = true;
    event.value = v;
}

 

A couple of tips:

  • Place this code in the Custom Keystroke Script for the date field (Properties > Format > Custom).

  • You may still want to add a validation script to check that the final date is valid (for example, not 02/30/2025).

  • Acrobat JavaScript runs in a sandboxed way, so testing step by step (typing slowly) helps you debug.

 

⚠️ Disclaimer: Adobe does not provide official support for custom JavaScript in PDFs. The script above is a community-suggested approach and represents the best possible support we can provide here in the forums.

 

Since you mentioned you’re a novice with Acrobat JavaScript, don’t worry. Experimenting with these small snippets is the best way to learn!

 

Best regards,
Tariq | Adobe Community Team | Meet Acrobat Studio

PDF Automation Station
Community Expert
Community Expert
August 22, 2025