Help with splitting an array only on first split

Baggies
Kilo Guru

I have a script that take a value from a text variable, and splits it into an array. It works fine..except, I want the firat array to go into the first text variable, and any subsequent arrays to go into a second text variable.

So, if I get the value from VarA, I want to split it, and push it to  VarB and VarC

If the user enters AAA BBB CCC DDD EEE in VarA 

VarB shows AAA

VarC shows BBB CCC DDD  because I don't have enough arrays defined.
------------------------------------------------------------------------------------------------

If the user enters AAA BBB  in VarA 

VarB shows AAA

VarC shows BBB undefined undefined  because I only have two arrays - anything extra is undefined

What I would like to see is:

VarA = AAA

VarB = anything that come after the first split, no matter how many subsequent splits there are.
------------------------------------------------------------------------------------------------

Many thanks for all replies.....Mark S.

Script is:

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var full_name = g_form.getValue('VarA');
var array = full_name.split(" ");
var firstname1 = array[0];
var lastname2 = array[1];
var lastname3 = array[2];
var lastname4 = array[3];
g_form.setValue('VarB', array[0]);
g_form.setValue('VarC', lastname2 + " " + lastname3 + " " + lastname4);

}

 

1 ACCEPTED SOLUTION

This works just fine now. Many, many thanks!! This is the finished script:

find_real_file.png


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

var full_name = g_form.getValue('account_display_name');
var array = full_name.split(" ");
var firstname1 = array[0];

var x='';
for(var i=1;i<firstname1.length;i++)

{

x=x+' '+array[i];

}
g_form.setValue('u_first_name', array[0]);
g_form.setValue('u_last_name', full_name.split(array[0])[1].trim());
g_form.setReadOnly('u_first_name', true);
g_form.setReadOnly('u_last_name', true);

}

View solution in original post

11 REPLIES 11

Ravi T
Tera Guru

Hi,

Try the below code

 

var full_name = g_form.getValue('VarA');
var array = full_name.split(" ");
var firstname1 = array[0];

var x='';
for(var i=1;i<firstname1.length;i++)

{

x=x+' '+array[i];

}
g_form.setValue('VarB', array[0]);
g_form.setValue('VarC', x);

 

Regards

Ravindra

 

Hello Ravinda, many thanks for you reply. Lets see how this works out in reality. This is a screen shot of what is happening with the new solution..you can see the  44444 and 5555555 is being cut off. My new script is below the screenshot.

Cheers, Mark

 

find_real_file.png

 

 

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

var full_name = g_form.getValue('account_display_name');
var array = full_name.split(" ");
var firstname1 = array[0];

var x='';
for(var i=1;i<firstname1.length;i++)

{

x=x+' '+array[i];

}
g_form.setValue('u_first_name', array[0]);
g_form.setValue('u_last_name', x);
g_form.setReadOnly('u_first_name', true);
g_form.setReadOnly('u_last_name', true);


}

Hi,

Can you just check the length of the field and get back

Regards

Ravindra

Abhinay Erra
Giga Sage

Here you go

var full_name = g_form.getValue('VarA');
var array = full_name.split(" ");

g_form.setValue('VarB', array[0]);
g_form.setValue('VarC', full_name.split(array[0])[1].trim());