输入类型忽略maxlength =“数字”在Chrome中 [英] maxlength ignored for input type="number" in Chrome
问题描述
maxlength
属性不适用于< input type =number>
。这只会发生在Chrome。 < input type =numberclass =test_cssmaxlength =4id = flight_numbername =number/>
From < input>
的MDN文档
如果类型属性的值是
text
,电子邮件
,搜索
,密码
,tel
或url
,此属性指定用户可以输入的最大字符数(以Unicode代码点计) ;对于其他控制类型,它将被忽略。
因此 maxlength
会被忽略< input type =number>
by design。
根据您的需要,您可以使用(注意:这将会在 c> min <只定义一个约束范围,而不是值的实际字符长度,但-9999到9999将覆盖所有0-4数字数字),或者您可以使用常规文本输入并使用新的 pattern
属性:
< input type =textpattern =\ d *maxlength =4 >
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"/>
From MDN's documentation for <input>
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.
So maxlength
is ignored on <input type="number">
by design.
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屋!