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 8988821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:06:00+00:00 2026-06-15T22:06:00+00:00

I need functionality that textarea should contain max 5 lines and each line should

  • 0

I need functionality that textarea should contain max 5 lines and
each line should contain max 15 chars, when user writes 2 words
e.g. 123456 123456789 and if that line char limit exceeds 15, then
it should bring the 2nd word in next line along with \n char in first line
(means 123456 will be in first line along with \n and 123456789 will be in 2nd)
,
I need to maintain \n(replacing <br>) in my db for some reasons.

i wrote this code, which gives fuzzy result in some conditions

<textarea onkeypress="charCountTextarea('txt1',event,'75','14')" id="txt1"></textarea> 

var countLines=0;

var newLines;

function charCountTextarea(textAreaId,e,limit,lineLen)
{   

       newLines = $("#"+textAreaId).val().split("\n").length;
       var t = $("#"+textAreaId)[0];
       var lineIndex = t.value.substr(0, t.selectionStart).split("\n").length-1;

    //console.log("new lines"+lineIndex);
        if(newLines >= 5 && $("#"+textAreaId).val().split("\n")[lineIndex].length>lineLen)
        {

            if( e.keyCode!=8 && e.keyCode!=46 && e.keyCode!=33 && e.keyCode!=34 && e.keyCode!=35 && e.keyCode!=36 && e.keyCode!=37 && e.keyCode!=38 && e.keyCode!=39 && e.keyCode!=40)
            {
                e.preventDefault();
                return false;
            }
        }


        else
        if($("#"+textAreaId).val().split("\n")[lineIndex].length>=lineLen)  // which will count the total line char condition
        {
            console.log($("#"+textAreaId).val().split("\n")[lineIndex][lineLen-1]);


            if($("#"+textAreaId).val().split("\n")[lineIndex][lineLen-1].indexOf(" ")==-1 && e.keyCode!=8 && e.keyCode!=46 && e.keyCode!=33 && e.keyCode!=34 && e.keyCode!=35 && e.keyCode!=36 && lineIndex != 4 && newLines<5)
            {
                // to bring the word in next line
                var str = $("#"+textAreaId).val(), replacement = '\n';
                str = str.replace(/ ([^ ]*)$/,replacement+'$1');
                $("#"+textAreaId).val(str);

            }
            else
            if(e.keyCode!=8 && e.keyCode!=46 && e.keyCode!=33 && e.keyCode!=34 && e.keyCode!=35 && e.keyCode!=36 && lineIndex!=4 && newLines<5)
            {
                // to insert next line              
                insertTextAtCaret(document.getElementById(textAreaId), "\n");
            }


        }

        if(e.keyCode == 13 && newLines >= 5) 
        {
            //linesUsed.css('color', 'red');
            e.preventDefault();
            return false;
        }

}
  • 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. Editorial Team
    Editorial Team
    2026-06-15T22:06:02+00:00Added an answer on June 15, 2026 at 10:06 pm
    function charCountTextarea(textAreaId,e,limit,lineLen)
    {   
    
        var code = e.charCode || e.keyCode;
    
        newLines = $("#"+textAreaId).val().split("\n").length;
        var t = $("#"+textAreaId)[0];
        var lineIndex = t.value.substr(0, t.selectionStart).split("\n").length-1;
        console.log('val t :'+$("#"+textAreaId).val()+' line index : '+lineIndex+' new lines '+newLines);
    
        if(lineIndex == 10 && $("#"+textAreaId).val().split("\n")[lineIndex].length>(lineLen+1) && code!=8 && code!=46 && code!=33 && code!=34 && code!=35 && code!=36 && code!=37 && code!=38 && code!=39 && code!=40)
        {
            $("#"+textAreaId).val(($("#"+textAreaId).val()).substring(0, $("#"+textAreaId).val().length - 1));
            alert('You are reached to limit');
            return false;
        }
    
        if(lineIndex<5)
        {
            $("#"+textAreaId).val($("#"+textAreaId).val().wordWrap(lineLen, "\n", 0));
        }
        var countLine1 = $("#"+textAreaId).val().split("\n")[0].length;
    
        if($("#"+textAreaId).val().split("\n")[lineIndex].length>lineLen)  // which will count the total line char condition
        {
            console.log("In condition : ");
            if(code!=8 && code!=46 && code!=33 && code!=34 && code!=35 && code!=36 && code!=37 && code!=38 && code!=39 && code!=40)
            {
                // to insert next line              
                insertTextAtCaret(document.getElementById(textAreaId), "\n");
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i need functionality that every character after 5th line in textarea will get deleted
I need to have functionality that would essentially emulate user creating a port forwrding
The functionality that I need is writing a header line at the beginning of
On my current project I need to add a functionality that allows the user
QQ: Hi I need (intuitive) functionality that allows a user to select multiple items
I am developing an IOS app. I need a functionality that includes undo and
I have some functionality that I need in all my classes which derive from
I have common functionality that I need to access from all screens of my
I need to add functionality to the close button in my class that extends
I need to access functionality from win32 dll , for that I am using

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.