Character restriction per line in the text area

I searched all weekend to solve this problem and have not yet found a solution that works correctly. What I'm trying to do is to limit the number of characters in a string in a text field - not limiting them equally, but a different number of characters in a string of my choice.

Example:

  • I want to have only 4 lines in my text box
  • Line 1,2 and 3 will be limited to 24 characters.
  • Line 4 will have an unlimited number of characters

Is this possible with textarea, or is there another way I can do it with a div or something else. I really understand that this, in all probability, was considered earlier, but finding the actual working script that covers this criterion proved to be extremely difficult, and I do not have the skill that is required to achieve these results.

thank

+5
source share
1 answer

The following is a sample snapshot of the problem you are trying to solve:

  • 4 lines in a text field (limit this to the text area itself with lines = "4")
  • Lines 1, 2, and 3 are limited to 24 characters.
  • Line 4 will have an unlimited number of characters

Text field snapshot:

123456789012345678901234
123456789012345678902333
232323232323232323323232
23232323232323232323236464536543654643

JavaScript:

$('#your-input').keypress(function() {
     var text = $(this).val();
     var arr = text.split("\n");

     if(arr.length > 5) {
         alert("You've exceeded the 4 line limit!");
         event.preventDefault(); // prevent characters from appearing
     } else {
         for(var i = 0; i < arr.length; i++) {
             if(arr[i].length > 24 && i < 3) {
                 alert("Length exceeded in line 1, 2, or 3!");
                 event.preventDefault(); // prevent characters from appearing
             }
         }
     }

     console.log(arr.length + " : " + JSON.stringify(arr));
});

. keypress , , \n .

  • , . , .
  • . for . 24, .
  • , .

, , , , . !

+6

All Articles