With the release of iOS5, Apple has added their own validation to input type="number" form fields. This is causing some issues; see this question below which sums it up:
Although input type="tel" works to bring up a numeric keypad on the iphone, it's the telephone keypad which has no decimal points.
Is there a way to set the numeric keypad as default using html/js? This not an iphone app. At minimum I need numbers and a decimal point in the keypad.
Update
Number input fields in Safari 5.1/iOS 5 only accept digits and decimal points. The default value for one of my inputs is $500,000. This is resulting in Safari showing a blank input field because $ , % are invalid characters.
Furthermore, when I run my own validation onblur
, Safari clears the input field because I'm adding a $ to the value.
Thus Safari 5.1/iOS5's implementation of input type="number" has rendered it unusable.
jsfiddle
Try it here - http://jsfiddle.net/mjcookson/4ePeE/ The default value of $500,000 won't appear in Safari 5.1, but if you remove the $ and , symbols, it will. Frustrating.
Why not put the $ outside the input field (as a label). Or the percent sign to the right of the input field? Seems like that'd solve your issue and you'd be able to use input type="number" and it'd just work.
This solution works on iPad and iPhone, and even Next/Previous work. Try http://fiddle.jshell.net/L77Cq/2/show/ (or http://jsfiddle.net/L77Cq/ to edit).
It still has the following issues: * If using forms, needs to copy value to a hidden field. * For some reason the page scrolls funny (iPhone address bar hides, iPad Debug Console hides).
PS: I found this technique independently of Ahmed Nuaman's comment above (I had tried it in the past, and noticed his comment just now - arrrgh!).
I just use something like
<input type="number" name="job_order_id" maxlength="7" size="10px" value="" />
and I get the keyboard with numbers and not the telephone keypad. You could also put the leading values into value, and they would already be there.Styling the field to contain symbols
When it comes to including the symbols within this
number
field, you can use some walkaround like that:HTML:
CSS:
Treat it as a proof of concept. See this jsfiddle for a live example. It looks like that in Chrome:
Defining smaller granularity
Based on the documentation on number input (Editor's Draft), to define granularity you need to add
step="<some-floating-point-number>"
attribute to the<input>
tag:and it will work in many modern browsers. See this jsfiddle for tests.
Conclusion
You should be able to style it to contain symbols you need. There is also a feature that, according to documentation, enables support for floating-point numbers.
Alternative solution - empty field in front of something else
You can also trick someone into believing he is seeing content of the input field, but show him something else behind the field (this is some extension of my original solution). Then if someone taps the field, you can propagate proper value etc., then go back to the previous state on blur. See this code (live example on jsfiddle - blue color means you are looking at something that is not within the field):
(the full code with styling is on the mentioned jsfiddle). The code needs shortening & cleaning up, but it is a proof of concept. Try it and share your findings, please.
We ended up using a hacky solution to enable the numbersandpunctuation keyboard to show when focused which works on an iPhone: we record the keypresses and simulate the value.
https://output.jsbin.com/necuzoj/quiet
We tried changing the input type from type=number to type=text on focus, but it was unreliable: (1) the numeric keyboard didn't show 100% of the time, and (2) if a form was was longer than the screen then the auto scrolling to show the focused input was completely stuffed up (sometimes the focused input would be off-screen or under the virtual keyboard).
Showing the numeric keypad (e.g.
type=tel
) didn't allow the entry of a decimal point or colon.On an iPad you can just use
<input type=text pattern=[0-9]*>
to show the numbersandpunctiation keyboard, but that solution doesn't work for an iPhone (an iPhone shows the numeric keypad with no decimal point).