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

  • Home
  • SEARCH
  • 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 8624733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:36:17+00:00 2026-06-12T07:36:17+00:00

Possible Duplicate: What is the best way to emulate an HTML input “maxlength” attribute

  • 0

Possible Duplicate:
What is the best way to emulate an HTML input “maxlength” attribute on an HTML textarea?

My question is can a <textarea > be configured only to accept a certain number of characters? I can do this in the following JavaScript, but the person for whom I am designing this page does not want a status field.

My current way of doing things is I have a <textarea > that is part of a form that uses a nice JavaScript function to fill a message in a status box.

I also read SO and found Alerts and sounds really aren’t the way to alert people, so the following code changes background color (and restores when appropriate) when there is an error.

Here is that code:

// Checks Form element TransDesc for overruns past 255 characters.
function warnOverDescLen()
{
  var misc_text  = 
  document.forms["InvGenPayTickets"]["TransDesc"].value;
  var alert_text = 
  "You cannot enter more than 255 characters. Please remove some information.";

  var rc = true;

  if(255 < misc_text.length)
  {
    document.forms["InvGenPayTickets"]["trans_status"].value 
      = alert_text;

    misc_text = misc_text.substring(0, 253);

    document.forms["InvGenPayTickets"]["TransDesc"].value 
      = misc_text;

    document.forms["InvGenPayTickets"]["trans_status"].style.backgroundColor 
      = "pink";
  }
  else
  {
    document.forms["InvGenPayTickets"]["trans_status"].value 
      = "";
    document.forms["InvGenPayTickets"]["trans_status"].style.backgroundColor 
      = "lightgoldenrodyellow";
  }

  return rc;
}

<textarea rows="4" cols="60" name="TransDesc" id="TransDesc" 
onkeypress="return warnOverDescLen();" ></textarea>

<span style="color: #50081E; font-weight: bold">Status</span>
<br />
<input type=text name="trans_status" id="trans_status" maxwidth="50"
size="65" />
  • 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-12T07:36:18+00:00Added an answer on June 12, 2026 at 7:36 am

    This is a solution for HTML5, not supported by IE9 or earlier (according to this):

    <textarea maxlength="255"></textarea>
    

    Since you probably can’t drop support for IE9 (and maybe even IE8), it’s recommended you couple that with JavaScript, preventing the default behavior for the keydown and paste events on the textarea, as standup75 suggested.

    Here is how to do that with plain JavaScript:

    <textarea id="txtarea" maxlength="255"></textarea>
    <script>
    var field = document.getElementById('txtarea');
    if(field.addEventListener) {
        field.addEventListener('keydown', enforceMaxlength);
        field.addEventListener('paste', enforceMaxlength);
    } else {
        // IE6-8
        field.attachEvent('onkeydown', enforceMaxlength);
        field.attachEvent('onpaste', enforceMaxlength);
    }
    
    function enforceMaxlength(evt) {
       var maxLength = 255;
        if(this.value.length >= maxLength) {
            evt.preventDefault()
        }
    }
    </script>
    

    http://jsfiddle.net/snJwn/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Best way to find Browser type & version? In HTML/JavaScript, How can
Possible Duplicate: Best way to stop SQL Injection in PHP my question is how
Possible Duplicate: Best way to programmatically detect iPad/iPhone hardware How can I differentiate the
Possible Duplicate: Best way to parse RSS/Atom feeds with PHP how I can grab
Possible Duplicate: best way to get the key of a key/value javascript object foo
Possible Duplicate: Best way to stop SQL Injection in PHP I am creating a
Possible Duplicate: Best way to copy the entire contents of a directory in C#
Possible Duplicate: Best way to stop SQL Injection in PHP I have seen some
Possible Duplicate: Best way to stop SQL Injection in PHP I need to secure
Possible Duplicate: Best way to programmatically detect iPad/iPhone hardware For example I want my

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.