Custom Form Text field, trying to insert "-" (dash) in real-time during keystrokes
I created a custom JS script for a text field that auto formats to ##-##-##-### . On display in the field, i have it light grey 00-00-00-000 giving the user foresight of the proper formatting. Once they click inside the field, it disappears and begins typing in 100% black, and if they only type in at least 2 numeric values, it will automatically add the trailing dashes and zeros (e.g. type” 23” the result will be 23-00-00-000). I was hoping to have the formatting displayed in realtime during keystrokes so when someone types in 1234, immediately once they keystroke 2 it will display 12- , with the first trailing dash. My field is a Cost Code (ID CC1 , 1 being the 1st row). Here are my inputs (and if I’ve over coded feel free to offer a simplified method). I’ve tried to have AI modify the code, but it ends up breaking what I’ve written.
Appearance Tab
Font Size: 8
Color: light grey
Options Tab
Default Value
00-00-00-000
Limit of 12 characters
Format Tab
Custom Format Script (blank)
Custom Keystroke Script:
// CC1 Keystroke (NO Validate tab):
// - Allow digits only (dashes allowed if pasted; we strip them)
// - Limit to 9 digits total
// - On commit (leaving field): pad 2/4/6 to 9 digits with zeros, then format DD-DD-DD-DDD
if (!event.willCommit) {
// If placeholder is still present for any reason, select all so typing replaces it
if (event.value === "00-00-00-000") {
event.selStart = 0;
event.selEnd = event.value.length;
}
// Force black while user types
if ((event.change || "").length > 0) {
event.target.textColor = color.black;
event.target.display = display.visible;
}
// Keep digits only from what user typed/pasted
var chg = (event.change || "").replace(/\D/g, "");
// Compute how many digits are currently in field (ignore dashes)
var curDigits = (event.value || "").replace(/\D/g, "");
// Digits in selection (so replacement math works)
var selPart = (String(event.value).substring(event.selStart, event.selEnd)).replace(/\D/g, "");
var capacity = 9 - (curDigits.length - selPart.length);
if (capacity < 0) capacity = 0;
// Trim to remaining capacity
if (chg.length > capacity) chg = chg.substring(0, capacity);
// Allow only digits to be inserted
event.change = chg;
event.rc = true;
} else {
// Commit-time: pad & format
var d = (event.value || "").replace(/\D/g, "");
// If user cleared everything, allow blank; OnBlur will restore placeholder/noPrint
if (d.length === 0) {
event.value = "";
event.rc = true;
} else if (d.length === 2 || d.length === 4 || d.length === 6 || d.length === 9) {
// Pad to 9 digits
if (d.length !== 9) d = (d + "000000000").substring(0, 9);
// Final format DD-DD-DD-DDD
event.value =
d.substring(0,2) + "-" +
d.substring(2,4) + "-" +
d.substring(4,6) + "-" +
d.substring(6,9);
event.target.textColor = color.black;
event.target.display = display.visible;
event.rc = true;
} else {
app.alert("Enter 2, 4, 6, or 9 digits (dashes optional).");
event.rc = false;
}
}Actions Tab
On Focus
// CC1 On Focus: clear placeholder and force black typing
if (event.target.value === "00-00-00-000") {
event.target.value = "";
}
event.target.textColor = color.black;
event.target.textSize = 8;
event.target.display = display.visible;On Blur
// CC1 On Blur: restore placeholder when empty; do not print placeholder
if (event.target.value === "") {
event.target.value = "00-00-00-000";
event.target.textColor = color.ltGray;
event.target.textSize = 8;
event.target.display = display.noPrint; // visible on screen, not printed
} else {
event.target.textColor = color.black;
event.target.textSize = 8;
event.target.display = display.visible;
}
