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.

How to replace characters in text field

Michael Bachme1
Kilo Guru

I have a requirement that any [spaces] in a text field in a Service Catalog item be replaced with semi-colons (;).   What's the best way to do this?

1 ACCEPTED SOLUTION

Use this one in onSubmit client script. not sure why it is throwing error in onChange.




function onSubmit() {


    //Type appropriate comment here, and begin script below



    var str = g_form.getValue('field_name');


  var res = str.replace(/ /g, ";");


g_form.setValue('field_name',res);


   


}


View solution in original post

10 REPLIES 10

Use this one in onSubmit client script. not sure why it is throwing error in onChange.




function onSubmit() {


    //Type appropriate comment here, and begin script below



    var str = g_form.getValue('field_name');


  var res = str.replace(/ /g, ";");


g_form.setValue('field_name',res);


   


}


It's working now. I missed the additional var line.



How do you had multiple replacements? I'd like to also replace carriage returns with a semi-colon.


Hi michael.bachmeyer



Here you go



var str = g_form.getValue('field_name').toString();


g_form.setValue('field_name',str.replace(/\s/g, ";"));


That did it, thanks!


Never use / / as the pattern for space. Always used \s meta character to find all the white spaces.