X
HOME HTML CSS JAVASCRIPT SQL PHP JQUERY ANGULAR XML ASP.NET MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

Input Text maxLength Property

Input Text Object Reference Input Text Object

Example

Get the maximum number of characters allowed in a specific text field:

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

The result of x will be:

30

Try it yourself »


Definition and Usage

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

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

Tip: To set or return the width of a text field, in number of characters, use the size property.


Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

The maxLength property is supported in all major browsers.


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 a text field:

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

Try it yourself »

Example

Jump to the next text field when a field's maxlength has been 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 »


Related Pages

HTML reference: HTML <input> maxlength attribute


Input Text Object Reference Input Text Object

Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB HOSTING

UK Reseller Hosting

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates FREE Website Templates Free HTML Templates

SHARE THIS PAGE

facebook