Input Text maxLength property
Example
Get the maximum number of characters allowed in the text field:
var x = document.getElementById("myText").maxLength;
x The output result is:
30
Try it »
Definition and Usage
The maxLength property can set or return the value of the maxlength property of the text field.
The maxLength property specifies the maximum number of characters allowed in the text field.
Tip: To set or return the width of the text field, use size attribute.
Browser support

All major browsers support the disabled attribute.
Syntax
Return the maxLength property:
textObject
.maxLength
Set the maxLength property:
textObject
.maxLength=
number
Attribute values
| Value | Description |
|---|---|
| number | Specifies the maximum number of characters allowed in the text field |
Technical details
| Return value: | Number, representing the maximum number of characters that can be entered in the text field |
|---|
More examples
Example
Set the maximum number of characters allowed in the text field:
document.getElementById("myText").maxLength = "4";
Try it »
Example
Automatically jump to the next text field to continue input after the characters entered in the text field reach the maximum:
if (y.length == x.maxLength) {
var next = x.tabIndex;
if (next < document.getElementById("myForm").length) {
document.getElementById("myForm").elements[next].focus();
}
}
Try it »
Related pages
HTML Reference Manual: HTML maxlength property
The Input Text object