can i set a maxlegth of characters when i am using textarea? if yes who i can do it?
Thank you very much

Dani AI

Generated

There was some confusion earlier between and because the HTML spec changed. Today the practical approach is to rely on the HTML5 maxlength attribute where available and add a small client-side fallback so older browsers and paste/IME scenarios are handled reliably. The formal reference for the element and its attributes is documented on MDN and in the HTML specification (MDN: textarea, HTML spec: textarea element).

A robust, simple fallback uses the input and composition events to cut any excess characters (this handles typing, paste, and IME input). Example (vanilla JS) — attach to your textarea element and set max to the allowed number:

// fallback enforcement for older browsers and IME composition
(function(){
  var ta = document.getElementById('myTextarea');
  if (!ta) return;
  var max = 200;
  var composing = false;
  ta.addEventListener('compositionstart', function(){ composing = true; });
  ta.addEventListener('compositionend', function(){
    composing = false;
    if (ta.value.length > max) ta.value = ta.value.slice(0, max);
  });
  ta.addEventListener('input', function(){
    if (!composing && ta.value.length > max) ta.value = ta.value.slice(0, max);
  });
})();

Notes and troubleshooting: a common reason it "did not work" is a typo in the attribute name or testing in an old browser; double-check spelling and browser version. For multiline text use textarea (it supports rows/cols or CSS sizing); input type="text" is single-line. Always enforce length on the server as well — client-side limits can be bypassed.

Recommended Answers

All 8 Replies

Yes, all you need to do is set the maxlength property in the textarea.

<textarea name="mytextarea" maxlength=100>

i' ve tried this but it did not work any idea why it doesn't work? Thank you very much

Can I see the form? It would make it much simpler to determine where the problem is.

Unfortunately, textarea doesn't support maxlength.
You can use javascript to limit the length

Are you familiar with jQuery?

Unfortunately, textarea doesn't support maxlength.
You can use javascript to limit the length

Are you familiar with jQuery?

You are right. I checked the HTML DOM and there is no maxlength property. If you know JavaScript or JQuery, both of those would be viable solutions.

not i am not familiar with jQuery. :( if i use input with type text i can set the rows and cols?

Using input means you're stuck with 1 row

that's true :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.