Input Text maxLength Attribute

Definition and Usage

maxLength The attribute sets or returns the value of the maxlength attribute of the text field.

The HTML maxLength attribute specifies the maximum number of allowed characters in a text field.

Tip:To set or return the width of the text field (in characters), use size attribute.

See also:

HTML Reference Manual:HTML <input> maxlength Attribute

Instance

Example 1

Get the maximum number of allowed characters in the specified text field:

var x = document.getElementById("myText").maxLength;

Try it yourself

Example 2

Set the maximum number of allowed characters in the text field:

document.getElementById("myText").maxLength = "4";

Try it yourself

Example 3

Jump to the next text field when the maximum length of the field is reached:

if (y.length == x.maxLength) {
  var next = x.tabIndex;
  if (next < document.getElementById("myForm").length) {
    document.getElementById("myForm").elements[next].focus();
  }
}

Try it yourself

Syntax

Return the maxLength attribute:

textObject.maxLength

Set maxLength attribute:

textObject.maxLength = number

Attribute Value

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.

Browser Support

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
Support Support Support Support Support