Expand my Community achievements bar.

Pasting a long string breaking them into single character across a rows of text fields

Avatar

Level 1

Hi,

I am trying to make a form with LiveCycle where there are many  textfields and when the user type in a sequence of characters the cursor will auto tab across the fields.  That can be accomplished with the below code.

topmostSubform.Page1._2_3[0]::change  - (JavaScript, client)

var temp = xfa.event.newText;if (temp.length  == 1){xfa.host.setFocus(_3)}

The second part is when the user tries to copy and past a string into the first text field I would like the string to break into characters and populate across the rows of textfields (one character per text field).

Any help would be greatly appriciated!

4 Replies

Avatar

Level 2

bump.

Anyone have ideas for this one?

Avatar

Level 6

I'll take a stab, but I'm not sure what you're trying to accomplish here.  So if the user pastes the string "ABCDE" into the first field, you want to have field1="A", field2 = "B", etc?  What happens if they paste the string into one of the other fields?  How do the characters get distributed?

Also, are all of the fields the same length, and what's the max length?

Avatar

Level 1

It would be nice if the string can always be distributed begining from the first textbox no matter where it's pasted within the rows of textboxes.  Also each textbox can only hold a single character and they are all set up this way.

-Thanks in advance for taking a stab.

Avatar

Level 6

There are issues with this, but it can be done.  Some of it will depend on you setting up your form correctly.

First, if the text fields are all set up to hold a max of 1 character, you can't use event.newText to get the pasted string, since newText returns the truncated text.  Instead, you need to use event.fullText.

Second, the process becomes much easier if you name all of your text fields the same, with a suffix of the occurance of the field starting from zero. So: "Textfield0", "Textfield1", etc.  I think you already headed down that path.

Third, make sure you truncate the pasted string so that the length is not greater than the number of text boxes.  My example below uses the string.slice() method to truncate. This assumes that there are 8 text boxes, you would change the "8" in the slice method to whatever your count is.

Based on the above, you can use a loop in the else section to fill in the fields.  The script is exactly the same for all text boxes since you're always starting the fill from the first textfield, so you can probably break this out into a global script method and eliminate duplicating the code.

else

     {

     var enteredText = xfa.event.fullText.slice(0, 8);

     for (var i = 0; i < enteredText.length; i++)

          {

          xfa.resolveNode("TextField" + i.toString()).rawValue = enteredText.substr(i, 1);

          }

     }  

There are other issues to deal with, depending on how your form is supposed to function.  For example, you will probably want to clear out the remaining text fields if a string that is shorter than the number of text fields is pasted.  That can be done with a small mod to the loop above.

I think you might also have a problem if the user pastes a string into a field that already contains a character.  The fullText value will include that character plus the pasted text.  You can check event.prevText and adjust the string if necessary.

Hopefully this will get you going.

Kevin