- 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-06-2025 07:09 AM - edited 05-06-2025 07:09 AM
It would be same. Create separate On change client script for both the fields as below.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == oldValue) {
return;
}
if (newValue != '') {
var trimmedAssetTag = newValue.replace(/^\s+|\s+$/g, '');
g_form.setValue('asset_tag', trimmedAssetTag);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2025 07:17 AM
That's what I thought. Ok Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2025 06:54 AM
Hi @purdue
If your requirement is to just remove whitespaces from start and end of string then JavaScript standard TRIM function would be enough.
Here is working example:
var cleanedName=" THIS IS TEXT with WHITE SPACE AT START and END ";
gs.print("Before:"+cleanedName);
var result =cleanedName.trim();
gs.print("After:"+result);
Output:
Regards,
Abhijit
ServiceNow MVP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2025 07:16 AM
Ok Thanks I will try this out as well.
Chad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2025 11:47 AM
Hello the final solution is located here.
Thanks,
Chad