Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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();
Copy link to clipboard
Copied
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');
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Just to clarify, max character limit should be 16 (including 3 hyphens) or 13 (excluding 3 hyphens)
Copy link to clipboard
Copied
Please type an example of the field completely filled out.
Copy link to clipboard
Copied
NAFID1-25-M-0009
Copy link to clipboard
Copied
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();
Copy link to clipboard
Copied
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');
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Interesting! Thank you!
Also what is the significance of the ! in 'if(!event.value)'?
Copy link to clipboard
Copied
It's a logical operator that means NOT. In this case, it means that true will be returned if the new value is empty.
Copy link to clipboard
Copied
So ! is the java equivalent of NOT in excel vba?
Copy link to clipboard
Copied
Yes, pretty much.
Copy link to clipboard
Copied
Only this is JavaScript, not Java... The two have a similar name but are quite different.
Copy link to clipboard
Copied
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');
Copy link to clipboard
Copied
Will you please mark my answer correct?
Copy link to clipboard
Copied
Yes sir my apologies!
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
Unfortunately not. It's a keystroke script. Once it's completed and accepted you can't modfiy it.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now