maxLength
Property
Example
Retrieve the maximum number of characters allowed in a text field:
var x = document.getElementById("myText").maxLength;
x output result:
30
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 the text field, use the size property.
Browser Support
All major browsers support the disabled
property.
Syntax
Return the maxLength
property:
Set the maxLength
property:
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 the text field can contain | | --- | --- |
More Examples
Example
Set the maximum number of characters allowed in a text field:
document.getElementById("myText").maxLength = "4";
Example
Automatically move to the next text field when the maximum number of characters is reached:
if (y.length == x.maxLength) {
var next = x.tabIndex;
if (next < document.getElementById("myForm").length)
{
document.getElementById("myForm").elements[next].focus();
}
}
Related Pages
HTML Reference: HTML <input> maxlength Attribute