输入类型=“数字"时忽略maxlength在 Chrome 中 [英] maxlength ignored for input type="number" in Chrome
问题描述
maxlength
属性不适用于 .这只发生在 Chrome 中.
The maxlength
attribute is not working with <input type="number">
. This happens only in Chrome.
<input type="number" class="test_css" maxlength="4" id="flight_number" name="number"/>
推荐答案
如果type属性的值为text
、email
、search
、password
、tel
或 url
,该属性指定用户可以输入的最大字符数(以 Unicode 代码点为单位);对于其他控件类型,它被忽略.
If the value of the type attribute is
text
,search
,password
,tel
, orurl
, this attribute specifies the maximum number of characters (in Unicode code points) that the user can enter; for other control types, it is ignored.
因此 maxlength
在 上被设计忽略了.
So maxlength
is ignored on <input type="number">
by design.
根据您的需要,您可以使用 min
和 max
属性作为 inon 在他/她的回答中建议的(注意:这将仅定义受限范围,而不是值的实际字符长度,尽管 -9999 到 9999 将涵盖所有 0-4 位数字),或者您可以使用常规文本输入并使用新的 pattern
属性:
Depending on your needs, you can use the min
and max
attributes as inon suggested in his/her answer (NB: this will only define a constrained range, not the actual character length of the value, though -9999 to 9999 will cover all 0-4 digit numbers), or you can use a regular text input and enforce validation on the field with the new pattern
attribute:
<input type="text" pattern="d*" maxlength="4">
这篇关于输入类型=“数字"时忽略maxlength在 Chrome 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!