Input Text maxLengthProperty
Examples
Get the maximum number of characters allowed in the text field:
var x = document.getElementById("myText").maxLength;
xThe output result is:
30
Try it »
Definition and Usage
The maxLength property sets or returns the value of the maxlength attribute of a text field.
The maxLength property specifies the maximum number of characters allowed in the text field.
Tip:To set or return the width of a text field, use thesizeattribute.
Browser Support
![]()
All major browsers support the disabled attribute.
Syntax
Return the maxLength property:
textObject.maxLength
Set the maxLength property:
textObject.maxLength=number
Property Values
| Value | Description |
|---|---|
| number | Specifies the maximum number of characters allowed in the text field |
Technical Details
| Return Value: | A number, representing the maximum number of characters allowed 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 number of characters:
if (y.length == x.maxLength) {
var next = x.tabIndex;
if (next < document.getElementById("myForm").length) {
document.getElementById("myForm").elements[next].focus();
}
}
var next = x.tabIndex;
if (next < document.getElementById("myForm").length) {
document.getElementById("myForm").elements[next].focus();
}
}
Try it »
Related Pages
HTML Reference Manual:HTML <input> maxlength Attribute
Input Text Object Other Extensions