- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2025 01:36 PM
Hello,
I have a requirement to remove whitespace from Asset tag and Serial Number on Alm_asset via Client Script. I was given regex
cleanedName = cleanedName.replace(/^\u200B+/, '').replace(/\u200B+$/, '');
Any assistance is appreciated.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2025 08:57 PM
you should use onChange client script rather than onSubmit script
something like this in onSubmit if you still require that
function onSubmit() {
// Get the values of the asset_tag and serial_number fields
var assetTag = g_form.getValue('asset_tag');
var serialNumber = g_form.getValue('serial_number');
// Check if asset_tag is not empty and trim whitespace
if (assetTag != '') {
var trimmedAssetTag = assetTag.replace(/^\s+|\s+$/g, '');
g_form.setValue('asset_tag', trimmedAssetTag);
}
// Check if serial_number is not empty and trim whitespace
if (serialNumber != '') {
var trimmedSerialNumber = serialNumber.replace(/^\s+|\s+$/g, '');
g_form.setValue('serial_number', trimmedSerialNumber);
}
}
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-05-2025 05:07 PM
The regex /\u200B+$/ matches the Unicode zero-width space from the end of a string. To remove all whitespace from a string use /[\s\u200B]+/g.
Your code, updated to remove all of the whitespace from the asset tag, would look like this:
function onSubmit() {
var tag = g_form.getValue('asset_tag');
if (tag != '') {
var trimmedValue = tag.replace(/[\s\u200B]+/g, '');
tag = trimmedValue;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2025 08:17 PM
Hi @purdue
If your requirement is just to remove white space, then try below script.
function onSubmit() {
var tag = g_form.getValue('asset_tag');
if (tag) {
var trimmedValue = tag.replace(/\s+/g, '');
g_form.setValue('asset_tag', trimmedValue);
}
}
Regards,
Siva
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2025 08:57 PM
you should use onChange client script rather than onSubmit script
something like this in onSubmit if you still require that
function onSubmit() {
// Get the values of the asset_tag and serial_number fields
var assetTag = g_form.getValue('asset_tag');
var serialNumber = g_form.getValue('serial_number');
// Check if asset_tag is not empty and trim whitespace
if (assetTag != '') {
var trimmedAssetTag = assetTag.replace(/^\s+|\s+$/g, '');
g_form.setValue('asset_tag', trimmedAssetTag);
}
// Check if serial_number is not empty and trim whitespace
if (serialNumber != '') {
var trimmedSerialNumber = serialNumber.replace(/^\s+|\s+$/g, '');
g_form.setValue('serial_number', trimmedSerialNumber);
}
}
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-06-2025 06:37 AM
Hello,
Thanks for this. Can you please give solution for change client script just in case.
Thanks again.
Chad