Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 127567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:29:50+00:00 2026-05-11T05:29:50+00:00

I’m looking for a javascript that can limit the number of lines (by line

  • 0

I’m looking for a javascript that can limit the number of lines (by line I mean some text ended by user pressing enter on the keyboard) the user is able to enter in textarea. I’ve found some solutions but they simply don’t work or behave really weird. The best solution would be a jquery plugin that can do the work – something like CharLimit, but it should be able to limit text line count not character count.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T05:29:50+00:00Added an answer on May 11, 2026 at 5:29 am

    This might help (probably be best using jQuery, onDomReady and unobtrusively adding the keydown event to the textarea) but tested in IE7 and FF3:

    <html>   <head><title>Test</title></head>   <body>     <script type='text/javascript'>       var keynum, lines = 1;        function limitLines(obj, e) {         // IE         if(window.event) {           keynum = e.keyCode;         // Netscape/Firefox/Opera         } else if(e.which) {           keynum = e.which;         }          if(keynum == 13) {           if(lines == obj.rows) {             return false;           }else{             lines++;           }         }       }       </script>     <textarea rows='4' onkeydown='return limitLines(this, event)'></textarea>   </body> </html> 

    *Edit – explanation: It catches the keypress if the ENTER key is pressed and just doesn’t add a new line if the lines in the textarea are the same number as the rows of the textarea. Else it increments the number of lines.

    Edit #2: Considering people are still coming to this answer I thought I’d update it to handle paste, delete and cut, as best as I can.

    <html>  <head>     <title>Test</title>     <style>         .limit-me {             height: 500px;             width: 500px;         }     </style> </head>  <body> <textarea rows='4' class='limit-me'></textarea>  <script>     var lines = 1;      function getKeyNum(e) {         var keynum;         // IE         if (window.event) {             keynum = e.keyCode;             // Netscape/Firefox/Opera         } else if (e.which) {             keynum = e.which;         }          return keynum;     }      var limitLines = function (e) {         var keynum = getKeyNum(e);          if (keynum === 13) {             if (lines >= this.rows) {                 e.stopPropagation();                 e.preventDefault();             } else {                 lines++;             }         }     };      var setNumberOfLines = function (e) {         lines = getNumberOfLines(this.value);     };      var limitPaste = function (e) {         var clipboardData, pastedData;          // Stop data actually being pasted into div         e.stopPropagation();         e.preventDefault();          // Get pasted data via clipboard API         clipboardData = e.clipboardData || window.clipboardData;         pastedData = clipboardData.getData('Text');          var pastedLines = getNumberOfLines(pastedData);          // Do whatever with pasteddata         if (pastedLines <= this.rows) {             lines = pastedLines;             this.value = pastedData;         }         else if (pastedLines > this.rows) {             // alert('Too many lines pasted ');             this.value = pastedData                 .split(/\r\n|\r|\n/)                 .slice(0, this.rows)                 .join('\n ');         }     };      function getNumberOfLines(str) {         if (str) {             return str.split(/\r\n|\r|\n/).length;         }          return 1;     }      var limitedElements = document.getElementsByClassName('limit-me');      Array.from(limitedElements).forEach(function (element) {         element.addEventListener('keydown', limitLines);         element.addEventListener('keyup', setNumberOfLines);         element.addEventListener('cut', setNumberOfLines);         element.addEventListener('paste', limitPaste);     }); </script> </body> </html> 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.