使HTML5输入类型=" number"接受破折号 [英] Make HTML5 input type="number" accepting dashes

查看:168
本文介绍了使HTML5输入类型=" number"接受破折号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用HTML表单的数字输入类型。

I want to use the number input type for my HTML form.

不幸的是,它只接受实数,之间没有破折号。

Unfortunately it only accepts real numbers, no dashes between.

有没有办法让数字输入接受1234-123456789这样的数字?

Is there a way to make the number input accepting numbers like "1234-123456789" ?

推荐答案

我最近有同样的问题。我通过使用type =tel而不是type =text或type =number来解决它。通过使用'tel',我能够在移动设备上获得仅数字键盘,并且仍然能够使用我的模式=[0-9 \ - ] +。

I recently had the same issue. I got around it by using type="tel" instead of type="text" or type="number". By using 'tel' I was able to get a numeric only keyboard on mobile devices and still be able to use my pattern="[0-9\-]+".

这是我使用的代码。我希望这对你需要的东西足够好。他们确实需要这样做,以便添加一个模式属性会覆盖由type属性设置的任何内置模式。

Here is the code I used. I hope this is good enough for what you need. They really need to make it so that adding a pattern attribute overrides any built in pattern set by the type attribute.

<input id='zipcode' name='zipcode' type='tel' pattern="[0-9\-]+" placeholder='Zip-code'>

当然,只有你想要的只是破折号和括号时才会有效。

Of course this will only work if all you want is dashes and possibly parentheses.

这篇关于使HTML5输入类型=&quot; number&quot;接受破折号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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