views:

5259

answers:

5

I'm experimenting with PhoneGap to develop some iPhone apps. PhoneGap basically wraps a UIWebView - it works well. The problem is the my app has several input fields that only take numeric input. I really need to force the numeric keypad instead of accepting the default standard keyboard, which then forces the user to switch to the numeric on every field. Does anyone know if this is possible? How?

Clarification: I know that Apple doesn't currently provide any API to allow this. I'm wondering if creating a custom class that inherited from UIWebView might help, or maybe creating a custom keyboard would open up some possibilities?

Update 6 Nov, 2009 - As noted below, Apple has recently updated safari functionality so that this is, once again, supported. So the accepted answer was changed to reflect that.

 <html>
<input type='tel'/>
<input type='number'/>
<input type='email'/>
<input />
</html>

The above now shows the following, in order:

  • alt text
  • alt text
  • alt text
  • alt text
+1  A: 

This was possible in the first iteration of the iPhone OS - Safari would give numeric keyboards to form fields with 'zip' or 'phone' in their names - but it disappeared in iPhone OS 2.0.

PhoneGap doesn't have an API function to override this at the moment. It may be something they're working on, it'd definitely be a nifty feature.

ceejayoz
Apparently possible, once again.
JJ Rohrer
Yeah, they've added it back in 3.1 it seems. Finally! What an odd thing to remove.
ceejayoz
+2  A: 

From Apple's documentation (the note about UIWebView):

You cannot specify the keyboard type in input elements. The web view displays a custom keyboard that is based on the default keyboard but includes some additional controls for navigating between form elements.

ashcatch
+2  A: 

Checked on IPhone OS 3.0, this functionality was still not there, not sure why Apple just removed this handy functionality, really upset to work on this right now :(

Leon Guan
+11  A: 

It looks like Mobile Safari supports the new HTML5 input type attributes of email, number, search, tel, and url. These will switch the keyboard that is displayed. See the type attribute.

So for example, you could do this:

<input type="number" />

And when the input box has focus, the number keyboard is shown (as if the user had the full keyboard and hit the "123" button.

If you really only want numbers, you could specify:

<input type="tel" />

And then the user would get the phone number dialing keypad.

I know this works with Mobile Safari -- I only assume it will work with UIWebView.

Chris Huseman
thankyou chris...was trying to find a way to do this for ages!
paulthenerd
Looks like type=number works great! Thanks!
tb
Sweet update. Its funny when things like this sneak back into the stack.
JJ Rohrer
This is also discussed on the apple dev forum: https://devforums.apple.com/thread/20425?tstart=0
JJ Rohrer
A: 

You need to make the change in the HTML portion of your Dashcode project. In Dashcode open index.html in the code window. On the canvas of the view you are working on, select the input field that you want to set to use the optimized keyboard. Click on index.html to give it focus. Select edit>find from the Dashcode menu bar. Type into the find box the exact name you gave to the text field control. Find will locate the control in the index.html file.

Change the type from type="text" to type="number"

Done.

JAnton