html模式仅接受数字 [英] html pattern to only accept numbers
问题描述
我有这个简单的html:
I have this simple html:
<input type="text"
placeholder="onlynumbers"
name="onlynumbers"
pattern="\d{1,5}"
maxlength="5">
我需要将字符数限制为5.可以.但是我仍然可以输入数字以外的其他字符.我尝试了各种模式,例如:
I need to restrict the number of characters to 5. It works. But I still can type other characters than numbers. I have tried various pattern, for example:
pattern = "[0-9]"
但是仍然可以键入数字以外的其他字符.
But it is still possible to type other characters than digits.
如果将 type
属性更改为 number
,则在输入字段中仅接受数字,但 maxlength
属性不起作用.我可以输入任意多个数字.我听说这是Chrome中的错误.那是问题吗?
If I change the type
attribute to number
, then only digits are accepted in the input field but the maxlength
attribute doesn't work. I can enter as many digits as I want. I heard this was a bug in Chrome. Is that the problem?
是否有跨浏览器的方式可以满足这些要求:
Is there a cross-browser way to fulfil these requirements:
仅接受数字并且不允许用户输入超过5位数字的输入字段(文本或数字,无关紧要).
推荐答案
<input type="text" name="country_code" pattern="[0-9]+" title="please enter number only" required="required">
这篇关于html模式仅接受数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!