HTML attribute: maxlength - HTML | MDN (original) (raw)

Try it

<label for="name">Product name:</label>
<input
  id="name"
  name="name"
  type="text"
  value="Shampoo"
  minlength="3"
  maxlength="20"
  required />

<label for="description">Product description:</label>
<textarea
  id="description"
  name="description"
  minlength="10"
  maxlength="40"
  required></textarea>
label {
  display: block;
  margin-top: 1em;
}

input:valid,
textarea:valid {
  background-color: palegreen;
}

Description

Constraint validation

While the browser will generally prevent user from entering more text than the maxlength attribute allows, should the length be longer than the maxlength allows, the read-only tooLong property of a ValidityState object will be true.

Examples

<input type="password" maxlength="4" />

Specifications

Specification
HTML # attr-input-maxlength
HTML # attr-textarea-maxlength

Browser compatibility

html.elements.input.maxlength

html.elements.textarea.maxlength

See also