HTML 输入字段强制编号
是否可以创建一个输入字段,将默认输入字符集设置为手机上的数字(以便数字键盘弹出)?
例如,为了更轻松地在 HTML 表单中输入电话号码。
Is it possible to create an input field that sets the default input character set to numbers on a mobile phone (so the NUMERICAL KEYBOARD POPS UP)?
For example to make it easier type in a telephone number into a HTML form.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
要使输入数字更容易,请使用
。要更轻松地输入电话号码,请使用
。并非所有手机都支持它们,但 iPhone 至少会默认为您提供数字键盘,而不是普通键盘。请参阅规范和< a href="http://diveintohtml5.ep.io/forms.html#type-number" rel="noreferrer">深入了解 HTML5 了解更多信息。
To make inputing numbers easier, use
<input type="number">
. To make entering phone numbers easier, use<input type="tel">
. Not all phone will support them, but the iPhone at least will give you a numeric keypad by default instead of the normal keyboard. See the spec and Dive Into HTML5 for more information.可以限制“手机”进入
手机表单录入使用
此处输入可以使用format="*N"进行限制
It is possible to limit entry on a "mobile phone"
The mobile phone form entry uses
input here can be limited using format="*N"
您可以使用
。这是 HTML5 的一项新功能。较旧的浏览器将简单地默认为文本输入字段。
You can use
<input type='tel'>
. This is a new HTML5 feature. Older browsers will simply default to a text input field.因此,您可以使用
type="tel"
或type="numbers"
。区别在于,一个尝试调出手机拨号键盘,而另一个则简单地切换到移动键盘的数字输入。
So you can use either
type="tel"
ortype="numbers"
.The difference is that one tries to bring up your phone dial keyboard and other simply switches to the numbers input of your mobile keyboard.
请参阅我的项目,使用 JavaScript 语言对网页上的文本输入元素的值进行跨浏览器过滤: 输入键过滤。代码示例:
另请参阅我的页面“自定义过滤器:”输入键过滤器示例
Please see my project of the cross-browser filter of value of the text input element on your web page using JavaScript language: Input Key Filter . Code example:
Also see my page "Custom filter:" example of the input key filter
这是一个使用 Javascript 的示例。这将只允许来自数字键盘/键盘顶部的数字和格式化程序(Shift/Backspace/等)的数字。您还可以考虑添加一个
setTimeout()
,有几秒钟的超时时间,以便onchange
事件检查是否有人将非数字粘贴到字段以及服务器端验证。示例
http://jsfiddle.net/vce9s/
Here's an example with Javascript. This will only allow numbers from the numpad/numbers on top of the keypad, and formatters (shift/backspace/etc). You may also consider adding a
setTimeout()
, with a couple seconds timeout, for theonchange
event to check in case someone pastes non numbers into the field as well as server side validation.Example
http://jsfiddle.net/vce9s/
对于我的测试“您可以使用
type="tel"
或type="numbers"
。”在 iPhone 上
type="tel"
只会调出数字键盘(如电话),而type="numbers"
会将数字键盘切换为数字和符号,因此两者工作,仅取决于您的应用程序。对我来说,我只需要数字,所以我使用type="tel"
来简化使用,效果非常好!for my testing of "you can use either
type="tel"
ortype="numbers"
."on iPhone
type="tel"
brings up the numbers only keypad (like phone) andtype="numbers"
brings up the numeric keypad switched to numbers and symbols, so both work, just depends on your application. For me I only need numbers so I usedtype="tel"
for ease of use and it worked great!