HTML 属性:maxlength
maxlength
属性定义了用户可以在 <input>
或 <textarea>
中输入的字符串长度的最大值。该属性的值必须是非负整数。
字符串长度是以 UTF-16 码元为单位进行计算的,通常(对于大多数语言脚本)等同于字符个数。如果未指定 maxlength
,或指定了无效值,则输入将没有最大长度限制。
任何 maxlength
的值必须大于或等于 minlength
的值(如果存在且有效)。如果字段文本值的长度超过了 maxlength
的 UTF-16 码元长度,输入将无法通过约束验证。约束验证仅在用户更改值时应用。
约束验证
虽然浏览器通常会阻止用户输入超过 maxlength 属性允许的文本长度,但如果输入的长度确实超出了 maxlength 的限制,ValidityState
对象的 tooLong
属性会返回 true。
尝试一下
示例
html
<input type="password" maxlength="4" />
规范
Specification |
---|
HTML # attr-input-maxlength |
HTML # attr-textarea-maxlength |
浏览器兼容性
html.elements.input.maxlength
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
maxlength |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
html.elements.textarea.maxlength
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
maxlength |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
- See implementation notes.
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.