You are viewing limited content. For full access, please sign in.

Question

Question

Limit number of lines and characters per line in multiline field

asked on February 28, 2018

I asked a similar question a couple of weeks ago, but need some more help.

I am working on a form that eventually populates a fillable PDF.  I have some comment boxes set as multi-line fields, but I need to ensure that whatever comments are written on the form will fit within the space allocated in the PDF.  I was going to just use maximum chars, but then realized that if a user puts carriage returns in the form it could cause those comments to exceed to space of the PDF and get cut off.  So I am trying to get code to work to limit both the number of lines and the number of characters in each line.

I found this thread that looks helpful, but I have been unable to get the code to work in Forms.  https://stackoverflow.com/questions/14259580/textarea-with-limited-lines-and-char-limits

There is discussion there about issues with the curser jumping around using the orignal answer, and Boris supposedly solved it with this code

var textarea = document.getElementById("splitLines");
textarea.onkeyup = function() {
  var lines = textarea.value.split("\n");
  var start = textarea.selectionStart;
  var end = textarea.selectionEnd;
  for (var i = 0; i < lines.length; i++) {
    if (lines[i].length <= 16) continue;
    var j = 0; space = 16;
    while (j++ <= 16) {
      if (lines[i].charAt(j) === " ") space = j;
    }
    lines[i + 1] = lines[i].substring(space + 1) + (lines[i + 1] ? " " + lines[i + 1] : "");
    lines[i] = lines[i].substring(0, space);
  }
  textarea.value = lines.slice(0, 6).join("\n");
  if (start == end) {
    textarea.setSelectionRange(start, end);
  }
};

But I am not familiar enough with Javascript to get the syntax to work in Forms though.  Can anyone help me out?

 

Thanks!

0 0

Replies

replied on February 28, 2018

OK, I actually just got it, but now my problem is that if I type 6 lines, then add a line at the top, the 6th line just disappears.  Fixing that is way beyond me.

 

//Limits comments field (Field192) to 6 lines, 16 characters per line
$(document).ready(function() {
var textarea = document.getElementById("Field192");
//textarea.onkeyup = function() {
$('.maxlinescharComments textarea').on('input focus keydown keyup', function() {
  var lines = textarea.value.split("\n");
  var start = textarea.selectionStart;
  var end = textarea.selectionEnd;
  for (var i = 0; i < lines.length; i++) {
    if (lines[i].length <= 16) continue;
    var j = 0; space = 16;
    while (j++ <= 16) {
      if (lines[i].charAt(j) === " ") space = j;
    }
    lines[i + 1] = lines[i].substring(space + 1) + (lines[i + 1] ? " " + lines[i + 1] : "");
    lines[i] = lines[i].substring(0, space);
  }
  textarea.value = lines.slice(0, 6).join("\n");
  if (start == end) {
    textarea.setSelectionRange(start, end);
  }
});
});

 

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.