Character Limit in HTML
Asked Answered
Y

6

96

How do you impose a character limit on a text input in HTML?

Yogh answered 22/9, 2008 at 5:59 Comment(0)
P
134

There are 2 main solutions:

The pure HTML one:

<input type="text" id="Textbox" name="Textbox" maxlength="10" />

The JavaScript one (attach it to a onKey Event):

function limitText(limitField, limitNum) {
    if (limitField.value.length > limitNum) {
        limitField.value = limitField.value.substring(0, limitNum);
    } 
}

But anyway, there is no good solution. You can not adapt to every client's bad HTML implementation, it's an impossible fight to win. That's why it's far better to check it on the server side, with a PHP / Python / whatever script.

Paoting answered 22/9, 2008 at 8:35 Comment(1)
Check on the server as a final sanity check, but add client-side enhancement if you can do so; it makes for a richer user experience.Joyance
T
44

there's a maxlength attribute

<input type="text" name="textboxname" maxlength="100" />
Thermaesthesia answered 22/9, 2008 at 6:1 Comment(3)
This is true, but some clients don't check this. This is especcially true for mobile phone based clients.Byway
There are also ways to remove them. For example the Firefox Web Developer Extension has a Remove Maximum lengths function.Antiphonal
With chrome and it's developer kit (which comes with the browser) it's simple to remove such things in the htmlLeo
F
12

In addition to the above, I would like to point out that client-side validation (HTML code, javascript, etc.) is never enough. Also check the length server-side, or just don't check at all (if it's not so important that people can be allowed to get around it, then it's not important enough to really warrant any steps to prevent that, either).

Also, fellows, he (or she) said HTML, not XHTML. ;)

Footrest answered 22/9, 2008 at 6:8 Comment(2)
i agree. one can POST data directly into a web site using some scripting tool, so in that case maxlength and other browser-side validations are not foolproofThermaesthesia
or use firebug and remove the maxlength attribute.Coif
W
5

use the "maxlength" attribute as others have said.

if you need to put a max character length on a text AREA, you need to turn to Javascript. Take a look here: How to impose maxlength on textArea in HTML using JavaScript

Washer answered 22/9, 2008 at 6:8 Comment(1)
check this link in StackOverflow: #1125982Estremadura
B
1

For the <input> element there's the maxlength attribute:

<input type="text" id="Textbox" name="Textbox" maxlength="10" />

(by the way, the type is "text", not "textbox" as others are writing), however, you have to use javascript with <textarea>s. Either way the length should be checked on the server anyway.

Brit answered 22/9, 2008 at 6:12 Comment(0)
C
0

you can set maxlength with jquery which is very fast

jQuery(document).ready(function($){ //fire on DOM ready
 setformfieldsize(jQuery('#comment'), 50, 'charsremain')
})
Cornela answered 1/10, 2015 at 11:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.