Max Length for Each Line in Textarea

Süleyman K

I use Angular 5.2.6 and adding some form file in my html. There is a textarea in that form and I want to limit its maximum chars in each row.

Here is the HTML part:

<textarea siInput [(ngModel)]="myTextarea" style="resize:none;  white-space: nowrap;" rows="20" (ngModelChange)="`handleTextareaChange`($event)" (keydown)="onKeydown($event)" maxlength="{{maxlength}}" id="textID"></textarea>

I have defined maxlength and maxLineLength in component class. Also here is the methods I have tried so far :)

onKeydown(event) {
   if (event.key === "Enter") {
     this.pressEnter = true;
     this.maxlength += this.maxLineLength + 1;
   }
   else {
     this.pressEnter = false;
   }
}

handleTextareaChange(text) {

   var mainString = text;
   var allLines = mainString.split("\n");

   var len = mainString.length;
   var numberOfLines = allLines.length;
   var indexOfCurrentLine = allLines.length - 1;

   var previousLines = "";
   for (var i = 0; i < indexOfCurrentLine; i++) {
     previousLines += allLines[i];
   }
}

If you have any ideas, please share with me.

Thanks.

Süleyman K

I have found the solution. I did not use maxlength properties, and created new function in component.ts

here is HTML:

<textarea siInput [(ngModel)]="myTextarea" style="resize:none;  white-space: both;" rows="20" (keyup)="onKeyAction()"(keydown)="onKeyAction()" id="textID" maxlength="{{maxTextAreaLength}}"></textarea>

and here is the funtion in component:

this.maxTextLineLength: number = 50;
onKeyAction() {
    if (this.myTextarea) {
      var lines = this.myTextarea.split(/(\r\n|\n|\r)/gm);
      for (var i = 0; i < lines.length; i++) {
        if (lines[i].length >= this.maxTextLineLength) {
          lines[i] = lines[i].substring(0, this.maxTextLineLength);
        }
      }
      this.myTextarea = lines.join('');
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Angular textarea matInput: how can set max length/line + max number of lines

jQuery each line in textarea

Codename One: TextArea input max length

display length of textarea value out of max

Getting length of each line in a list

Textarea with max number of lines and max characters per line

Line up input, select, textarea to each other

create if statement on each individual line of a textarea object

how to remove first character in each line of textarea

How to print each line in textarea with javascript?

calculate max length of each field in csv file

proper way to handle max line length python

Compile the kernel to increase the command line max length

Multi line Text box max length

Max line length for u-boot setenv?

Multiple vertical lines in PhpStorm for max line length

Textarea | val().length not counting "Enter/Line Breaks" in chrome

How can I drop length of input from textarea length and cap both max limits?

how to wrap each line inside a textarea by html tags?

Adding paragraph tags around each line in html textarea

Different background colors for each line in the textarea (or editable div?)

php read from textarea each line doesn't work why?

Django show list of strings in textarea, each string from new line

jQuery Textarea - Remove spacing but add line breaks after each word

Get each line from textarea that contains links with PHP

Trim and edit each line of textarea while user is typing

calculate max length of each field in csv file using groovy

Split very long line of words into separate lines of max length

What is the one line max length limit in XML files?