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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:09:30+00:00 2026-05-15T11:09:30+00:00

I got a strange requirement: function TextLimit(elem, maxChars) { … } Where elem is

  • 0

I got a strange requirement:

function TextLimit(elem, maxChars) { ... }

Where elem is a textarea or input of type=text and maxChars is the maximum number of characters that could go into the elem. The code inside the function must limit the length of the text inside elem to maxChars when the user is typing. However, if the user is pasting for the first time, the elem must take everything that was pasted even if the pasted text has more characters then maxChars.
EDIT: If the pasted text is longer then maxChars, then after pasting for the first time, the user can’t type/paste anymore, unless the text is deleted/backspaced to a length smaller then maxChars.

Any suggestions using plain JavaScript or jQuery would be appreciated.

The maxlenght attribute doesn’t allow for pasting. Setting/removing it programmaticaly on paste, doesn’t work well in all browsers. Taking substring of elem’s value on keydown/keypress/keyup produces some funky results.

  • 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-05-15T11:09:31+00:00Added an answer on May 15, 2026 at 11:09 am

    Something like this seems to work:

    Couple of things to work out still, like allowing the delete key if the max is reached. Should be easy fixes though.

    Try it out: http://jsfiddle.net/wxUZz/7/ (updated)

    function TextLimit(elem, maxChars) {
        var permittedKeys = [8,37,38,39,40,46];
    
        elem.focus(function() {
            if(elem.val().length >= maxChars) {
                elem.data('prevent', true);
                elem.data('value', elem.val());
            }
        });
    
        elem.blur(function() {
            if( elem.data('prevent') ) {
                elem.val( elem.data('value') );
            }
        });
    
        elem.keydown(function(event) {
            var count = elem.val().length;
            if(count >= maxChars && $.inArray(event.which, permittedKeys) < 0) {
                elem.data('prevent', true);
                elem.data('value', elem.val());
                return false;
            } else {
                elem.data('prevent', false);
            }
        });  
    }
    
    TextLimit($('#myTextarea'), 30);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got strange issue. I want wait end async call at sync function call.
Possible Duplicate: Why does PHP echo'd text lose it's formatting? I got strange problem.
I got a strange problem with split function over the String. Please look at
I got a strange behavior from the mail function in php here is the
Got really strange Firefox issue. Using the Microsoft __doPostBack javascript function in web forms
I got strange behavior for iOS App. My App crashes only when it is
I got strange behavior when I tried to test my navigator.geolocation.getCurrentPosition web page. Here
Got a strange problem created a little stored proc which need to execute a
I got a strange compilation error when I followed the MSDN document to use
I got a strange exception when trying to find out if a property in

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.