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

@purdue 

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

}

 

That's what I thought.  Ok Thanks!

Abhijit4
Mega Sage

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:

Abhijit4_0-1746539649575.png

 

 

 

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

Ok Thanks I will try this out as well.  

Chad

purdue
Kilo Sage