maxlength (HTML attribute)

Spec
Depr. Version
No HTML 2
Browser support (more…)
IE5.5+ FF1+ SA1.3+ OP9.2+ CH2+
Full Full Full Full Full

Syntax

maxlength="number"

Description

While it’s possible to use JavaScript to validate form data entry, this approach can’t be totally relied upon (JavaScript may be disabled, and form data may not be trustworthy if the client has been affected by a nefarious script). Server–side validation is a much safer option, and could be used, among other things, to check that the amount of data submitted isn’t too long. The maxlength attribute provides a means for reducing—but still not completely eliminating—the likelihood that too much data will be sent to the server for processing. It is primarily a usability enhancement, indicating to users when they’ve reached the maximum character length for an input, thus avoiding the chance of writing an essay only to be told afterwards that there is a max character count of 4.

Example

This maxlength attribute restricts entries in this password field to a maximum of four characters:

<form>
  <label for="pin">Your 4-digit PIN:</label>
  <input type="password" name="pin" id="pin"
      maxlength="4" size="6"/>
  ⋮
</form>

Value

The value for this attribute is a number which represents the total characters that can be submitted.

Compatibility

Internet Explorer Firefox Safari Opera Chrome
5.5 6.0 7.0 8.0 1.0 1.5 2.0 3.0 3.5 1.3 2.0 3.1 4.0 9.2 9.5 10.0 2.0
Full Full Full Full Full Full Full Full Full Full Full Full Full Full Full Full Full

It causes no compatibility issues, and has excellent support across all tested browsers.

User-contributed notes

Related Products