Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[zh-cn] Create translation of html maxlength attribute #24128

Merged
merged 10 commits into from
Oct 21, 2024
45 changes: 45 additions & 0 deletions files/zh-cn/web/html/attributes/maxlength/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
title: HTML 属性:maxlength
slug: Web/HTML/Attributes/maxlength
l10n:
sourceCommit: 2b112aef57df0649462db5d9f47d782a7aa1f25c
MaxtuneLee marked this conversation as resolved.
Show resolved Hide resolved
---

{{HTMLSidebar}}

`maxlength` 属性定义了用户可以在 {{htmlelement('input')}} 或 {{htmlelement('textarea')}} 中输入的最大[字符串长度](/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length)。该属性的值必须是 0 或更高的整数。

字符串长度是以 UTF-16 编码单元为单位进行测量的,通常([对于大多数语言脚本](/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length#strings_with_length_not_equal_to_the_number_of_characters))等同于字符数。如果未指定 `maxlength`,或指定了无效值,则输入没有最大长度限制。

任何 `maxlength` 的值必须大于或等于 [`minlength`](/en-US/docs/Web/HTML/Attributes/minlength) 的值(如果存在且有效)。如果字段文本值的长度超过了 `maxlength` 的 UTF-16 编码单元长度,输入将无法通过约束验证。约束验证仅在用户更改值时应用。

### 约束验证

虽然浏览器通常会阻止用户输入超过 `maxlength` 属性允许的文本长度,但如果输入的长度确实超出了 `maxlength` 的限制,{{domxref("ValidityState.tooLong", "tooLong")}} 属性会在 {{domxref("ValidityState")}} 对象中返回 `true`。
MaxtuneLee marked this conversation as resolved.
Show resolved Hide resolved

{{EmbedInteractiveExample("pages/tabbed/attribute-maxlength.html", "tabbed-shorter")}}

## 示例

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

{{EmbedLiveSample('Examples', '100%', 200)}}
MaxtuneLee marked this conversation as resolved.
Show resolved Hide resolved

## 规范

{{Specifications}}

## 浏览器兼容性

{{Compat}}

## 参见

- [`minlength`](/en-US/docs/Web/HTML/Attributes/minlength)
- [`size`](/en-US/docs/Web/HTML/Attributes/size)
- [`pattern`](/en-US/docs/Web/HTML/Attributes/pattern)
- [约束验证](/zh-CN/docs/Web/HTML/Constraint_validation)
- [表单数据校验](/zh-CN/docs/Learn/Forms/Form_validation)
MaxtuneLee marked this conversation as resolved.
Show resolved Hide resolved
- {{htmlelement('input')}}
MaxtuneLee marked this conversation as resolved.
Show resolved Hide resolved
Loading