How to Limit number of characters in a field and how to automatically add numbers as prefix?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-02-2025 10:34 AM - edited 01-06-2025 01:12 PM
Hi everyone,
I created a new catalog form with the following fields (refer to screenshot below). I need help with the following as I do not know how to properly execute the following:
1. On the Dock# field, we need to limit the character to just 7 digits.
2. If a user enters less than 7 digits, automatically add 0 as prefix on the WebURL field until it becomes 7 digits. The 0's will appear on the WebURL field and not on the Dock# field.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-02-2025 10:47 AM
onChange Client Script for the Dock# field
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
// Clear WebURL if Dock# is blank
if (newValue === '') {
g_form.hideFieldMsg('dock_number', 'error');
g_form.setValue('web_url', '');
return;
}
// Handle more than 7 digits
if (newValue.length > 7) {
g_form.showFieldMsg('dock_number', 'INVALID DOCK NUMBER', 'error');
return;
}
g_form.hideFieldMsg('dock_number', 'error');
// Handle less than 7 digits
if (newValue.length < 7) {
let paddedValue = newValue.padStart(7, '0');
g_form.setValue('web_url', paddedValue); // Update WebURL
} else {
g_form.setValue('web_url', newValue); // Set as is if it's 7 digits
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-02-2025 10:52 AM
Since this is a catalog item, I would recommend building in the data validation directly on the catalog item. I would do the following:
Limit the Dock Number field to 7 digits
You will do this by adding a variable attribute to your variable:
Add Help Text or Instructions to the Dock# field
On the same screen you go to add the max_length, but this time in the Annotation tab, you can add a help message/instruction text to your field. You could put the action to add prefix zeros on the user from here if you wanted.
Use a separate field for the Web URL and the Web URL w/ Dock#
Is the user entering the Web URL on the catalog item form? Are they also expected to input the Dock# with the URL? As in, will they know they need to add the prefixed zeros? Can you potentially have a separate field for Web URL (editable) and Web URL with Dock# (read-only), where the latter gets set automatically? You would achieve this by running an onChange Client Script, watching the Web URL field for input (and checking Dock# for a valid input), concatenating the two fields, and setting your read-only field with the output.
You can use the padStart method to add the prefixed zeros:
var dockNumber = "123"; // Example with the user adding 123 as the dock number
var paddedInput = dockNumber.padStart(7, '0'); // Adds a 0 until 7 characters is reached
console.log(paddedInput); // Output will be "0000123"