Count and/or limit lines used in a multiline input field?

Rather than counting/limiting inputs by the number of characters, is it possible to count/limit by the number of lines used in a multiline input field?

I will be interested to see how this could be approached.

Viewports can take on different widths and if your multiline input is set to be responsive … I do not see how this could be done.

Perhaps establishing a logic by which with a specific font, font size and fixating the MLI width you can infer how many lines (depending on characters count) have been inserted.

If by “lines” you mean “hard return” - i.e. someone hit the return key - as opposed to “soft wrapping”, then it seems doable, although it might require a little bit of “voodoo”. If I get a chance, I’ll experiment.


If the above is the case, then this has already been solved for the most part. See this post and this post. Basically, using regex, convert the input into a list of strings using carriage return as the delimiter; and the number of lines will be the list item count.

If you need to take into account lines created by soft wrapping, then that’s a whole different animal, and nothing comes to mind right off the bat.


1 Like

Yes, a hard return. Thanks, @sudsy

The use case is for making sure the user doesn’t create a “comment” that extends beyond the printable area. This can be difficult if the user uses hard returns, thereby making a character count limit all but useless. I’ll look at the posts you referenced.

@cmarchan It’s a fixed width multiline input, so there is some level of predictability.

Hi Robert,

Please review and advise

The problem is running off the printable area. Limiting the characters won’t prevent this if the user uses a lot of hard returns.

@sudsy Thanks. It works for counting the hard returns, but it doesn’t really solve the issue.

How about this…
If I can size the multiline input field on the form to exactly match the print layout, could it be possible to…

  1. with “Stretch to fit content” off, detect if the scroll bar is activated (meaning its beyond the natural dimensions of the input, or
  2. with “Stretch to fit content” on, detect if the multiline input is being stretched?

Just to approach this from a slightly different perspective, would it solve the problem if you simply stripped the newlines out of the text that was entered? Or do you not want to modify what the user has input?


Trying to avoid modifying the users input. Want it to be WYSIWYG.

This topic was automatically closed after 70 days. New replies are no longer allowed.