- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2024 07:17 AM
Hi,
I have an onChange client script on a List collector field based on the selection it will populate those names in another variable.
The names are populated as comma separated values and spaces are coming in between as shown below so I have used the replace function to remove the spaces
But it removes all the spaces rather than the spaces between names.
eg., Suppose a name X Y, Z so my output is XY,Z but i want it to be X Y,Z
Below is the onChange script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
g_form.clearValue('model_handle_list');
var apps = g_form.getDisplayValue('spm_test_name').replace(/\s+/g, "");
//g_form.setValue('model_handle_list', apps);
}
Any help/suggestions.
Thank you!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2024 07:53 AM
See the following example:
var stringVal = 'X Y, Z';
var arrayValues = stringVal.split(',');
var newStringArray = [];
for (i = 0; i < arrayValues.length; i++) {
newStringArray[i] = arrayValues[i].trim();
}
var newStringVal = newStringArray.toString();
gs.info('New value = ' + newStringVal);
I'm not sure how you do that with regex.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2024 09:40 AM
Hi @Shidhi ,
Please try the below:
var strVal = 'X Y Z, Z G';
(function validateModel(inputData) {
var formattedNewVal = inputData.replace(/\s*,\s*/g, ',');
gs.info(formattedNewVal);
})(strVal);
Output:
*** Script: X Y Z,Z G
Second Use Case:
var strVal = 'X Y, Z';
(function validateModel(inputData) {
var formattedNewVal = inputData.replace(/\s*,\s*/g, ',');
gs.info(formattedNewVal);
})(strVal);
Output:
*** Script: X Y,Z
Mark this as Helpful / Accept the Solution if this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2024 11:08 AM
In the second Use case I get:
var strVal = ' X Y, Z '; // has leading and trailing space
(function validateModel(inputData) {
var formattedNewVal = inputData.replace(/\s*,\s*/g, ',');
// var formattedNewVal = inputData.replace(/^\s+|\s+$/g, '');
gs.info('new val:'+formattedNewVal+'.');
})(strVal);
// results in:
*** Script: new val: X Y,Z .
leading and trailing spaces are not revmoved.
// next test:
var strVal = ' X Y, Z ';
(function validateModel(inputData) {
// var formattedNewVal = inputData.replace(/\s*,\s*/g, ',');
var formattedNewVal = inputData.replace(/^\s+|\s+$/g, '');
gs.info('new val:'+formattedNewVal+'.');
})(strVal);
results in:
*** Script: new val:X Y, Z.
with leading and trailing spaces removed.
Seem that @Shidhi
can use:
var apps = g_form.getDisplayValue('spm_test_name').replace(/^\s+|\s+$/g, '');
to get what is wanted.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2024 07:20 AM
Hi @Shidhi ,
Check if there is option to replace ", " with ","
the first one - comma and one space
the second one - only comma
-Thanks,
AshishKM
Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2024 07:53 AM
See the following example:
var stringVal = 'X Y, Z';
var arrayValues = stringVal.split(',');
var newStringArray = [];
for (i = 0; i < arrayValues.length; i++) {
newStringArray[i] = arrayValues[i].trim();
}
var newStringVal = newStringArray.toString();
gs.info('New value = ' + newStringVal);
I'm not sure how you do that with regex.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2024 09:40 AM
Hi @Shidhi ,
Please try the below:
var strVal = 'X Y Z, Z G';
(function validateModel(inputData) {
var formattedNewVal = inputData.replace(/\s*,\s*/g, ',');
gs.info(formattedNewVal);
})(strVal);
Output:
*** Script: X Y Z,Z G
Second Use Case:
var strVal = 'X Y, Z';
(function validateModel(inputData) {
var formattedNewVal = inputData.replace(/\s*,\s*/g, ',');
gs.info(formattedNewVal);
})(strVal);
Output:
*** Script: X Y,Z
Mark this as Helpful / Accept the Solution if this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2024 11:08 AM
In the second Use case I get:
var strVal = ' X Y, Z '; // has leading and trailing space
(function validateModel(inputData) {
var formattedNewVal = inputData.replace(/\s*,\s*/g, ',');
// var formattedNewVal = inputData.replace(/^\s+|\s+$/g, '');
gs.info('new val:'+formattedNewVal+'.');
})(strVal);
// results in:
*** Script: new val: X Y,Z .
leading and trailing spaces are not revmoved.
// next test:
var strVal = ' X Y, Z ';
(function validateModel(inputData) {
// var formattedNewVal = inputData.replace(/\s*,\s*/g, ',');
var formattedNewVal = inputData.replace(/^\s+|\s+$/g, '');
gs.info('new val:'+formattedNewVal+'.');
})(strVal);
results in:
*** Script: new val:X Y, Z.
with leading and trailing spaces removed.
Seem that @Shidhi
can use:
var apps = g_form.getDisplayValue('spm_test_name').replace(/^\s+|\s+$/g, '');
to get what is wanted.