There are several tricks for displaying different keyboards on mobile devices for HTML 5 inputs (i.e. <input>
tags).
For example, some are documented on Apple's website, Configuring the Keyboard for Web Views.
These are great for usability, but when it comes to an input for for international postal codes (mostly numeric, but letters allowed), we're left with some poor options. Most people recommend using the pattern="\d*"
trick to show the numeric keyboard, but that doesn't allow for letter input.
The type="number"
input type shows the regular keyboard but shifted to the numeric layout:
This works well for iOS devices, but it makes Chrome think the input must be a number and even changes the input's behavior (up/down increment and decrement the value).
Is there any way to get iOS to default to the numeric layout, but still allow for alphanumeric input?
Basically, I want the iOS behavior for type="number"
but I want the field to behave like a regular text field on desktop browsers. Is this possible?
UPDATE:
Sniffing the user-agent for iOS and using the type="number"
input type is not an option. type="number"
is not meant for string values (like postal codes), and it has other side effects (like stripping leading zeros, comma delimiters, etc) that make it less than ideal for postal codes.
Browsers currently have no proper way of representing numeric codes like postcodes and credit card numbers. The best solution is to use
type='tel'
which will give you a number keypad and ability to add any character on desktop.Type
text
andpattern='/d'
will give you a number keypad but only on iOS.There is an HTML5.1 proposal for an attribute called
inputmode
which would allow you to specify keypad regardless of type. However not is not currently supported by any browser.I would also recommend having a look at the Webshim polyfill library which has a polyfill method for these types of inputs.
An update to this question in iOS 11. You can get the number keypad by simply adding the pattern attribute (
pattern="[0-9]*"
) to any input with a number type.The following works as expected.
This also works.
@davidelrizzo posted part of the answer, but the comments from @Miguel Guardo and @turibe give a fuller picture but are easy to miss.
Try this:
I know this is very hackish way, but this apparently works.
I haven't tested on Android devices though.
Note this may causes a little noticeable glitches when
$this.attr('type', 'number')
because this reset the value wheninput
has non numerical characters.Basic ideas are stolen from this answer :)
This will make the numerical side of the ios keyboard display by default and maintains the ability to switch to the alphabetical side. When the html input type changes, the device changes the keyboard to match the appropriate type.
alternate demo: http://jsfiddle.net/davidcondrey/dbg1L0c0/3/embedded/result/
If you want the large numerical format keyboard (the telephone style) you can adjust the code accordingly and it still works:
A quick google search found this Stackoverflow question.
HTML
Javascript
The input type is switched before the form can be validated, showing the correct keyboard without messing up the value. If you only want it to run on iOS, you will probably have to use the user agent.
Stackoverflow on detecting iOS
Why not check the header of the HTTP request (user agent), and serve up the numeric layout to the iOS devices, while serving up the alphanumeric layout to the rest?