- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2025 08:53 PM
I have the submit client script below but message is disappearing after loading. Any assistance is appreciated. Thanks, Chad
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2025 11:43 AM
Hello All,
I want to thank everyone for their responses. We were able to resolve this. Below is the solution.
Ask
Filter Whitespace from 2 fields(Asset Tag and Serial Number) on Alm Asset.
Solution
Create 2 OnChange Client Scripts AND Before BR on Insert and Update with condition gs.Interactive for editing from list view.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2025 08:50 AM
why are you not using onChange client script and trimming and then setting the value?
OR
You can write the logic to trim after form submission
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2025 09:06 AM
I tried the OnChange but issue still persists. Just to be clear the message is displaying but disappears once the form loads. Please provide example to display message then trim after submission. Thanks, Chad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2025 09:31 AM
Hello @purdue
function onSubmit() {
var assetTag = g_form.getValue('asset_tag');
var serialNumber = g_form.getValue('serial_number');
var regex = /\s+|\u200b/g;
var trimmed = false;
// Trim asset tag
if (assetTag.match(regex)) {
g_form.setValue('asset_tag', assetTag.replace(regex, ''));
trimmed = true;
}
// Trim serial number
if (serialNumber.match(regex)) {
g_form.setValue('serial_number', serialNumber.replace(regex, ''));
trimmed = true;
}
if (trimmed) {
// Show confirmation popup and prevent immediate submit
var message = "We have trimmed your white space. Click OK to continue submitting.";
g_form.addInfoMessage(message);
// Show popup, wait for OK
setTimeout(function() {
var confirmed = confirm(message); // referred: https://www.w3schools.com/jsref/met_win_confirm.asp
if (confirmed) {
// Submit the form manually
g_form.submit();
}
}, 0);
return false; // Stop submission until user confirms
}
return true; // Proceed with submission if no trimming needed
}
Hope that helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2025 05:02 PM
Hello,
Thank for this. Is there anyway we can pop the message without the confirmation? They just want the user to see the message.
Thanks,
Chad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2025 09:39 AM
Hello @purdue
Try this onSubmit client script:
function onSubmit() {
var assetTag = g_form.getValue('asset_tag');
var serialNumber = g_form.getValue('serial_number');
var regex = /\s+|\u200b/g;
// Check if asset_tag is not empty and trim whitespace
if (assetTag.match(regex)) {
var trimmedAssetTag = assetTag.replace(regex, '');
g_form.setValue('asset_tag', trimmedAssetTag);
g_modal.alert("We have trimmed your white space");
}
// Check if serial_number is not empty and trim whitespace
if (serialNumber.match(regex)) {
var trimmedSerialNumber = serialNumber.replace(regex, '');
g_form.setValue('serial_number', trimmedSerialNumber);
g_modal.alert("We have trimmed your white space");
}
}
Note: Replacing g_form.addInfoMessage() with g_modal.alert()
Hope this helps!
"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"
Thank You
Juhi Poddar