Split a String using K2 Designer Expression

  • 22 September 2016
  • 4 replies
  • 45 views

Userlevel 3
Badge +16

Hi all,

 

Hoping someone can help.

 

Using K2 Designer expressions, how can i split the following address (in a TEXT BOX) into seperare parts?

 

123 K2 Avenue,K2Town,K2County,K21456

 

Unfortunately there is no SPLIT function when using K2 Designer

 

Is there a way of doing it using Javascript in an expression?

 

Thanks guys,


4 replies

Userlevel 3
Badge +16

Hi,

 

I've done some more work on this:

 

On Smartforms Designer, I have a data label, ticked literal and also using the following expression:

 

<script type = "text/javascript">

var str = "123 K2 Avenue,K2Town,K2County,K21456";
var strarray = str.split(",");
alert (strarray[0] +  "
" + strarray[1] + "
"  + strarray[2]  + "
" + strarray[3]);

</script>

 

This just pops up an alert with the seperate lines.

 

My question is, how do i get the Javascript to use the contents of a k2 Smartforms TextBox and pass it to the line: var str = and then display the result back into the Text box?

 

Anyone please?

Badge

I'll let others answer the question being asked, but as I assume this is related to the postcode discussion, I'll suggest a non-JS workaround here:

 

If you've got a defined number of fields in that list (i.e. it'll always contain 5 fields, each separated by a comma), could you achieve the result using a cascade of data labels and find/left/right operators? Not the most elegant solution, but might be faster than trying to traverse the DOM in javascript.

 

e.g.:

 

AddressList is your comma-separated address: 123 K2 Avenue,K2Town,K2County,K21456

Field 1 = Left(AddressList, Find(AddressList, ","))

Data label 1 = Right(AddressList, Length(AddressList) - Find (AddressList, ",")) - This then becomes everything to the right of the first comma

Field 2 = Left(Data label 1, Find(Data label 1, ","))

Data label 2 = Right(Data label 1, Length(Data label 1) - Find (Data label 1, ","))

...

etc.

 

You'll probably need to stick some +/-1's in there to get the indexing right.

 

 

Userlevel 3
Badge +16

Tom............. That works great when you have a fixed comma delimited string of returned addresses.

 

Only thing i noticed when looking up some random addresses is that some have 4 parts, others have 5

 

5 parts = BusinessName,Road,Town,County,Postcode

4 parts = Number+Road,Town,County,Postcode

 

Is there a way of checking how many commas there is from the string and then using your expressions to fill accordingly?

 

i.e.

 

1)Check string to see how many commas.....

2) If 5 commas,  then do a rule to set of expressions for 5 textboxes

3) If 4 commas, then do a rule to set expressions for 4 textboxes

Badge +2

 

Other way can be using javascript.

Use a hidden data label. Set its literal value to true(Check it in properties for the label). Then, you may use it to put your <script>code</script> in your datalabel's expression as below:

13898i7C555F77F6FA227A.png

 

In your javascript, you can then get hold of your element using name (avoid using id as it may change) through jquery/javascript and then focus the textbox and set it's text/val(pls check whichever is working) property.

 

Hope it helps!

Reply