Purpose : to find a cross-platform solution for displaying numeric keypads on mobile touch devices with minimal hacks.
Problem :
I have a regular web application using data entry forms containing mostly numeric data. When a user interacts with my site on a mobile device, I would like to display a digital virtual keyboard, since most standard keyboards require a second press to switch from alpha to numbers. I know that I can run a different keyboard by setting the "type" attribute of the input element:
type=number : This works great under iOS / Safari. I am not sure about other browsers on the platform.
In Android, this does not constantly raise the correct keyboard in different browsers and often leads to unwanted elevator buttons on the input. I have yet to find a clean way to disable them in CSS.
type=tel : This almost works on iOS / Safari, but there is no decimal button on the phone keypad.
It seems to work fine in multiple Android browsers, without adding additional user interface elements to the page.
My current solution is hacky and simplistic. Based on the class that I already use for numerical verification, I replace every text element that should contain a number with a new input, which is either the type number or tel based on the detected OS / browser.
var isAndroid = navigator.userAgent.match(/android/i) ? true : false; var isIOS = navigator.userAgent.match(/(ipod|ipad|iphone)/i) ? true : false; if (isAndroid || isIOS) { var useNumberType = (isIOS ? true : false); //iOS uses type=number, everyone else uses type=tel jQuery("input.num").each(function () { var type = (useNumberType ? "number" : "tel"); var html = this.outerHTML; html = html.replace(/(type=\"?)text(\"?)/, "$1" + type + "$2"); this.outerHTML = html; }); }
I would prefer not to use browser detection and not change inputs on the fly at runtime. I could imagine a server-side http module that did basically the same thing, but that is not substantially better. I am shocked that there is no CSS request for this.
Is there a better way to get a numeric keypad with a decimal button that works on all or most touch mobile devices without adding weird user interface elements to the page?
-------------- update
I donβt think there is a way to do what I really want to do, namely to configure one input style or type that will work well in desktop browsers and on all major mobile touch platforms. I decided to change the input type through a direct DOM call, and through jQuery instead of rewriting all the input through outerHTML. I suspect it doesn't make much difference, but the code is a little cleaner. Since I do not change the input types on the desktop, I do not need to worry about restricting IE reading to this attribute only.
Ideally, I would probably handle this on the server side so that everything is sent to the browser in the format required by the device making the request. But now the new code looks something like this:
var isAndroid = navigator.userAgent.match(/android/i) || navigator.platform.match(/android/i) ? true : false; var isIOS = navigator.userAgent.match(/(ipod|ipad|iphone)/i) ? true : false; if (isAndroid || isIOS) { var useNumberType = (isIOS ? true : false); //iOS uses type=number, everyone else uses type=tel jQuery("input.num").each(function () { var type = (useNumberType ? "number" : "tel"); if (this.type == "text") { this.type = type; } }); }