Add " - " symbol after every two numbers in string field.

ar1
Kilo Sage

HI All,
Can anyone please help us on the below requirement.

We have one field called "Count" and it's string type.
And when an end user enter any numbers in the field we need to automatically add the " - " symbol after every two numbers.

Examples:

-->  12-34-56
--> 32-3
--> 45

Advance thanks.

1 ACCEPTED SOLUTION

ar1
Kilo Sage

Hi All,
Many thanks for your response.

Below code working fine for us:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

// //Type appropriate comment here, and begin script below

var str = newValue.toString();

var tempdata='';
for (var i=0; i<str.length; i++){
if(str[i]!='-')
{
tempdata=tempdata+str[i];
//alert(tempdata);
}
}
//alert(tempdata);
var result='';
for (var j=0; j<tempdata.length; j++){
if(j%2!=0 && j!=tempdata.length-1)
{
result=result+tempdata[j]+'-';
}else{
result=result+tempdata[j];
}
}
//alert(result);

g_form.setValue('u_count', result);

}

View solution in original post

11 REPLIES 11

Hello,

The code is giving the results as expected but the g_form.setValue() is not working for the onchange field if i want to update this Newvalue to short description by setValue it working fine. May be there is exception in servicenow or may be there is another way to set the onchage filed in onchage client scripts.

 

Thanks

ar1
Kilo Sage

Hi All,
Many thanks for your response.

Below code working fine for us:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

// //Type appropriate comment here, and begin script below

var str = newValue.toString();

var tempdata='';
for (var i=0; i<str.length; i++){
if(str[i]!='-')
{
tempdata=tempdata+str[i];
//alert(tempdata);
}
}
//alert(tempdata);
var result='';
for (var j=0; j<tempdata.length; j++){
if(j%2!=0 && j!=tempdata.length-1)
{
result=result+tempdata[j]+'-';
}else{
result=result+tempdata[j];
}
}
//alert(result);

g_form.setValue('u_count', result);

}