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

Javascript code for a text box that only allows specific format numerically

Explorer ,
Sep 26, 2023 Sep 26, 2023

Hi all!

 

I am tryin to make a specific text box on an adobe acobat form be a specific format and only populate numbers. I need the text box to format as xx-xxxxx for a Bankruptcy Case Number. The text field needs to be able to change for multiple numbers. For example, if I need the text field to be 12-34567 and then the next time I compete the form I need it to say 98-76543. If I were to type anything other than this format, the text box won't populate - this would also include any letters that are typed, the text box would won't populate. Only format acceptable is xx-xxxxx. The dash must auto populate as well. 

 

Thanks for you help!

TOPICS
JavaScript , Modern Acrobat , PDF , PDF forms
1.5K
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
1 ACCEPTED SOLUTION
Community Expert ,
Sep 27, 2023 Sep 27, 2023

The simplest way is to use an Arbitrary Mask:

 

Capture_2309271121.png

 

Capture_2309271120.png


Acrobate du PDF, InDesigner et Photoshoptographe

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
Adobe Employee ,
Sep 26, 2023 Sep 26, 2023

Hi @gasperg62515419 

 

Hope you are doing well and thanks for reaching out.

 

Disclaimer: I want to clarify that I am not a JavaScript expert. Feel free to attempt implementing the provided code and assess its functionality for your specific situation.

 

// Get a reference to the text field by its name
var textField = this.getField("YourFieldName");

// Define a regular expression pattern for the allowed format (e.g., only digits and one decimal point)
var pattern = /^[0-9]+(\.[0-9]+)?$/;

// Add an event listener to the text field for user input
textField.setAction("Keystroke", function() {
// Get the current value of the text field
var value = event.value;

// Test the value against the pattern
if (!pattern.test(value)) {
// If the value doesn't match the pattern, clear the field
app.alert("Please enter a valid numerical format.");
event.value = "";
}
});

 

Make sure to replace "YourFieldName" with the actual name of your text field. This code will enforce that the text entered into the field matches the specified numerical format (digits with an optional decimal point). If the input doesn't match the pattern, it will display an alert and clear the field.

 

Hope it will help.

 

Regards

Amal

Regards
Amal
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
Explorer ,
Sep 26, 2023 Sep 26, 2023
Thanks! As I am not a coder, I need to remove the text with the // -
correct?

And to confirm, the only part I need to update is "YourFieldName" with the
actual name of my text box?
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
Explorer ,
Sep 26, 2023 Sep 26, 2023

@Amal. Thanks! As I am not a coder, I need to remove the lines with the "//"  correct?

 

Also is the only thing I need to update is the "YourFieldName" with my actual text box name? I only say one "YourFieldName"

 

Thanks!

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
Explorer ,
Sep 26, 2023 Sep 26, 2023

@Amal. 

 

var textField = this.getField("Case");

var pattern = /^[0-9]+(\.[0-9]+)?$/;

textField.setAction("Keystroke", function() {

var value = event.value;


if (!pattern.test(value)) {

app.alert("Please enter a valid numerical format.");
event.value = "";
}
});

 

"Case" is my test box

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 ,
Sep 26, 2023 Sep 26, 2023

Hi @gasperg62515419 

 

Yes, that is correct 'Case' is your test box. you can remove the lines with the "//" as those are the comments within the code for explaining and understanding.

 

~Amal

 

Regards
Amal
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
Explorer ,
Sep 26, 2023 Sep 26, 2023

@Amal.  Thanks! Thats what I thought but it doesnt seem to be working.

 

It still lets me type in letters and doesnt format to xx-xxxxx

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 ,
Sep 27, 2023 Sep 27, 2023

The simplest way is to use an Arbitrary Mask:

 

Capture_2309271121.png

 

Capture_2309271120.png


Acrobate du PDF, InDesigner et Photoshoptographe
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
Explorer ,
Sep 27, 2023 Sep 27, 2023

@JR Boulay  this worked perfectly! thanks!

 

one follow-up question. Is there a way to change the warning pop-up box? For Example, instead of saying "The value entered does not match the format of the field = "99-99999" it would say "The value entered does not match the format of the field = "xx-xxxxx"

 

Capture.JPG

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 ,
Sep 27, 2023 Sep 27, 2023

"It still lets me type in letters and doesnt format to xx-xxxxx"

This is normal, Amal.'s script doesn't format the value.


Acrobate du PDF, InDesigner et Photoshoptographe
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 ,
Sep 27, 2023 Sep 27, 2023
LATEST

"Is there a way to change the warning pop-up box? "

No, this requires to use a format function in JavaScript


Acrobate du PDF, InDesigner et Photoshoptographe
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