Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Client Script remove White Space from fields

purdue
Kilo Sage

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.

 
function onSubmit() {
var tag = g_form.getValue('asset_tag');
if(tag != '') {
var trimmedValue = tag.replace(/\u200B+$/'');
tag = trimmedValue;
}
}
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@purdue 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

9 REPLIES 9

KevinWhite
Tera Expert

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;
    }
}

 

J Siva
Kilo Patron
Kilo Patron

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

Ankur Bawiskar
Tera Patron
Tera Patron

@purdue 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hello,

Thanks for this.   Can you please give solution for change client script just in case.  

Thanks again.

Chad