数字+空格字符串(信用卡号)的移动友好输入

Mobile-friendly input of a digits + spaces string (a credit card number)(数字+空格字符串(信用卡号)的移动友好输入)
本文介绍了数字+空格字符串(信用卡号)的移动友好输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个信用卡号输入表单,移动浏览器和非移动浏览器都将使用它.

我想暗示一个键盘应该出现在移动设备上,并允许(可选)空格,所以应该接受4111 1234 1234 1234"或4111123412341234".

根据我的发现,选项是:

a) <input type="tel"/> - 这似乎符合我的要求(至少在当前的移动浏览器中),但它在语义上是错误的.

b) <input type="text" pattern="[d ]*"/> 或类似的 - iPhone 可以识别一些模式 ([0-9]*, d*)与使用键盘一样,但这在 Android 上效果不佳.此外,我不确定 iPhone 是否会提供任何允许空格的小键盘模式,尽管我现在手头没有 iPhone 可以检查.

c) 尝试使用 Javascript 进行浏览器检测并使用 (a) 用于移动设备和 (b) 用于非移动设备.杂乱无章,对 (a) 没有真正的好处.

<input type="number"/> 似乎是一个非首发,因为 Chrome 至少会将此类输入强制为数字,因此会使用空格中断输入.

有没有比使用 type="tel" 更好的方法来提示移动浏览器应该提供数字键盘?

也许是一个 -webkit-* 属性,可以应用于普通文本输入字段以提示应该显示小键盘?

解决方案

对于仍然可以包含空格和其他特殊字符的文本字段,语义正确的标记是 但是据我所知,inputmodework/multipage/association-of-controls-and-forms.html#attr-fe-inputmode" rel="noreferrer">WhatWG 目前尚不支持任何浏览器.其目的是在具有数字小键盘但仍充当文本输入的设备上为用户提供数字小键盘.

我的建议是用 JS 填充 inputmode 并在触摸设备上更改为 type="tel" (这是目前可用的最佳解决方案).请注意,不同的设备具有非常不同的电话"和数字"键盘,iOS 的电话"不允许使用空格,而 Chrome 移动版允许使用各种特殊字符.谁知道自定义 Android 键盘有什么作用?

如果你不想构建自己的 polyfill,你可以实现 Webshim 现在polyfills inputmode 从 release 1.14.0.这也有一个很好的特性,可以通过设置 pattern="[0-9]*" 如果你想要它来保持调用 iOS数字"键盘(注意:这不是你得到的同一个键盘当设置 type="number"!)

这是我对跨浏览器的输入类型行为和我的分析与@aFarkas(Webshim 作者)就 polyfill 输入模式进行辩论.p>

I have a credit card number input form that will be used by both mobile and non-mobile browsers.

I want to hint that a keypad should appear on mobile, and to allow (optional) spaces, so either "4111 1234 1234 1234" or "4111123412341234" should be accepted.

From what I've found, the options are:

a) <input type="tel"/> - this seems to behave as I want (with current mobile browsers at least), but it's semantically wrong.

b) <input type="text" pattern="[d ]*"/> or similar - the iPhone recognises some patterns ([0-9]*, d*) as working with the keyboard, but this doesn't work as well on Android. Also I'm not sure there are any patterns that the iPhone will give a numpad for that allow spaces, though I don't have an iPhone on hand to check right now.

c) Attempt browser detection with Javascript and use (a) for mobile and (b) for non-mobile. Cludgy, and no real benefit over (a).

<input type="number"/> seems to be a non-starter since Chrome at least will force such input to a number, therefore breaking input with spaces.

Is there a better way of hinting to mobile browsers that they should offer a numpad than using type="tel"?

Edit:

Maybe a -webkit-* property that could be applied to a normal text input field to hint that a numpad should be shown?

解决方案

The semantically correct markup for a text field that can still contain whitespace and other special characters is <input type="text" inputmode="numeric"/> however as far as I am aware while inputmode is recommended by WhatWG it is not yet supported by any browsers. Its intent is to present the user with a numeric keypad on a device that has it but still behave as a text input.

My suggestion would be to polyfill inputmode with JS and change to type="tel" on touch devices (which is the best solution currently available). Be mindful that differently devices have very different 'tel' and 'number' keypads, iOS 'tel' does not allow spaces while Chrome mobile allows all sorts of special characters. Who knows what custom Android keypads do?

If you don't want to build your own polyfill you can implement Webshim which now polyfills inputmode as of release 1.14.0. This also has has the nice feature of keeping the iOS 'number' keypad invoked by setting pattern="[0-9]*" if you want it (note: this is not the same keypad you get when setting type="number"!)

Here is the analysis I did on input type behavior across browsers and my debate with @aFarkas (the Webshim author) on polyfilling inputmode.

这篇关于数字+空格字符串(信用卡号)的移动友好输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Select OK button from N#39;th modal opened in testcafe(从 testcafe 中打开的第 N 个模式中选择 OK 按钮)
How to click a specified li for an autocomplete ul with Selenium IDE?(如何使用 Selenium IDE 为自动完成 ul 单击指定的 li?)
Test automation html element selectors. Element ID or DataAttribute(测试自动化 html 元素选择器.元素 ID 或 DataAttribute)
navigator.geolocation.getCurrentPosition not allowed on quot;file:///C:/quot; based access(“file:///C:/上不允许 navigator.geolocation.getCurrentPosition基于访问)
the server responded with a status of 429 (Too Many Requests) intlTelInput.js(服务器响应状态为 429 (Too Many Requests) intlTelInput.js)
Geolocation not working on Safari 5.x on Windows 7/XP(地理定位在 Windows 7/XP 上的 Safari 5.x 上不起作用)