Expand my Community achievements bar.

SOLVED

Limit maximum characters per line in TextField, help please

Avatar

Level 2

Hello Livecycle designers!

It would be great if you could help me me with the following problem. I have a TextField with 5 lines (limit to visible area, and accurate height) and I want to limit the maximum characters per line to 10 (and the total in this field to 50, but that I'm able to do with 'Limit length, max chars' option).

As far as I know that can be done only by scripting so, in change event I've written:

var     lines = xfa.event.newText.toString().split('\n');

     i=lines.length,
      maxChar = "10";

if (maxChar) {
      while(i-->0) {
           if(lines[i].length == maxChar) {
                xfa.event.change = "\n";
           }
      }
}

and actually that works great when instead of '\n' in the 1st line, I put any character (for example '|' and I will pretend that '|' is my new \n).

So the main problem is to split the user entered text into lines (sadly the '\n' is not working). So the solution to this problem would be somehow to split my Textbox into lines (in array) after user presses Enter.

I would appreciate any help here, so if you just have a 2 minutes of your spare time please help me.

1 Accepted Solution

Avatar

Correct answer by
Level 10

Hi,

I rebuild your idea ... that's the result.

var Input = xfa.event.fullText;

var FullLength = Input.length;

var SplitLength = 10

var SplitParts = Math.ceil(FullLength / SplitLength)

var JoinString = "";

for (var i=0; i <= SplitParts; i++)

{

SplitString = Input.substring(0, SplitLength);

Input = Input.replace(SplitString, "");

JoinString = JoinString + SplitString + "\u000a";

}

xfa.resolveNode("Textfield2").rawValue = JoinString;

View solution in original post

5 Replies

Avatar

Level 10

Hi,  The form will use a return character at the end of a line, so even though you assign a new line (that is "\n") it gets changed to a "\r".  Also your script will lose every tenth character (xfa.event.change = "\n" should use the += assignment) and only work on the first line (because of the decrement of i in the while statement).  Try something like;

var lines = xfa.event.newText.toString().split('\r');
var i=lines.length - 1;
var maxChar = 10;

if(lines[i].length == maxChar)
{
xfa.event.change += "\r";
}

Although, this will only work as people are typing in, what do you want to happen to line 4 if someone takes some characters out of line 3?

Good luck

Bruce

Avatar

Level 2

Thank you radzmar, altough it didn't quite work, I'm happy that you are taking part in this discussion.

Thank you BR001, your solution is really close, thank you for your input, I appreciate it.

As you said, this is working really nice (I've got some issues with the \r which is treated as a additional character) however the main problem is with copy\paste and what's even worse - the behaviour of my textfield after user come back to this field and start typing. Generally he can type as much as the field can accept.

I'm thinking about adding:

var lines = xfa.event.newText.toString().split('\r');
var i=lines.length - 1;
var maxChar = 10;

if(lines[i].length >= maxChar) {
   lines[i]=lines[i].slice(0,maxChar);
    xfa.event.change += "\r";
}

but it's not really working.. neither .substring is

Generally I just want to limit each line in the textfield to x characters (I was trying to accomplish that with a few textfields instead of one with multiply lines, but moving around those fields it's not really user friendly).

If someone takes out some characters that's fine, I don't want to give him a possibilty to add more characters (if he reached the limit per line). Or maybe we should try with different approach, I'm really looking forward to resolve this problem (actually I thought that's a common one?).

Regards

Avatar

Correct answer by
Level 10

Hi,

I rebuild your idea ... that's the result.

var Input = xfa.event.fullText;

var FullLength = Input.length;

var SplitLength = 10

var SplitParts = Math.ceil(FullLength / SplitLength)

var JoinString = "";

for (var i=0; i <= SplitParts; i++)

{

SplitString = Input.substring(0, SplitLength);

Input = Input.replace(SplitString, "");

JoinString = JoinString + SplitString + "\u000a";

}

xfa.resolveNode("Textfield2").rawValue = JoinString;

Avatar

Level 2

Thank you guys for your time and knoweldge