输入类型忽略maxlength =“数字”在Chrome中 [英] maxlength ignored for input type="number" in Chrome

查看:136
本文介绍了输入类型忽略maxlength =“数字”在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, email, search, password, tel, or url, 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆