Force iOS numeric keyboard with custom / currency pattern

For now, JavaScript is the only solution. Here’s the simplest way to do it (using jQuery):


<input type="text">


$('input[type="text"]').on('touchstart', function() {
  $(this).attr('type', 'number');

$('input[type="text"]').on('keydown blur', function() {
  $(this).attr('type', 'text');

The idea is simple. The input starts off and ends up with type=”text”, but it briefly becomes type=”number” on the touchstart event. This causes the correct iOS keyboard to appear. As soon as the user begins to enter any input or leave the field, the input becomes type=”text” once again, thus circumventing the validation.

There’s one downside to this method. When the user returns to an input that has already been filled out, the input will be lost (if it doesn’t validate). This means the user won’t be able to go back and edit previous fields. In my case, this isn’t all that bad because the user may want to use the calculator over and over again with different values, so automatically deleting the input will save them a few steps. However, this may not be ideal in all cases.

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.

Leave a Comment