14

Notice that using input type="number" can display a numeric keyboard as below:

enter image description here

Is it possible to use input type="text" to display the same numeric keyboard? I do not want to display a number pad using pattern="\d*" because it is possible that the value will contain a decimal place.

The reason I would like to use input type="text" instead of input type="number" is that I cannot get back the value if I input a non-number for a number field. For example, if I input ABC, it will become empty automatically. It seems to me that using input type="text" will be easier for this kind of control.

Captain Delano
  • 377
  • 3
  • 12
red23jordan
  • 2,663
  • 10
  • 34
  • 53
  • What's wrong with using `type="number"`? – deceze May 09 '13 at 07:06
  • Hi deceze, I have editted my question. Thanks. – red23jordan May 09 '13 at 07:17
  • 5
    There is, in HTML5.1, an attribute called ["inputmode"](http://www.w3.org/html/wg/drafts/html/master/forms.html#attr-fe-inputmode) which is the intended way of supporting this. So you'd have ``However, it's not listed in HTML5, so I guess it's not currently implemented much if at all. – Alohci May 09 '13 at 09:48
  • Solution here: http://stackoverflow.com/a/25599024/1922144 – davidcondrey Sep 01 '14 at 05:12
  • 3
    @Alohci `inputmode="numeric"` no longer works. – Ahmad Alfy Oct 03 '16 at 11:12
  • @AhmadAlfy - Has it ever worked? If so, in what browsers? – Alohci Oct 03 '16 at 11:41
  • @Alohci my mistake, I was confused that it was working on iOS Safari but turned out the numeric keyboard was showing because of the pattern attribute. I thought it was working because of `inputmode`. My bad – Ahmad Alfy Oct 03 '16 at 13:09

6 Answers6

17

If your input is a true number, integer or decimal then use the HTML5 type="number" input. This will bring up correct keyboard on Android devices (assume Windows phone too).

Then the trick is to place a pattern="[0-9]*" on that attribute to force the special numeric keypad on iOS. Note that:

  1. This will not mark a decimal or minus sign as invalid because the pattern attribute is actualy NOT valid on a type="number" field! and
  2. This is the ONLY way to get the iOS numeric keyboard. See difference between the number section of the alpha keyboard (as in your screenshot above) compared to the true numeric keyboard.

iOS number keyboards compared

One last note, be sure NOT TO use the type number field for inputs that are not true numbers (eg. zipcodes with leading zeros or product codes with comas or spaces). A numeric input field MAY NOT SUBMIT values that are not true numbers! (depending on browser/device)

davidelrizzo
  • 615
  • 7
  • 13
  • Using pattern="[0-9]*" doesn't bring up the proper number keyboard on Iphone it has no way to switch to alphabetic or symbols keyboard and there is no decimal point either – Ikram Shah Oct 18 '18 at 07:47
8

The numeric keyboard provided by Apple on iOS is sad joke. But, you can fix this using:

inputmode="decimal"

Work fine on Android, off course.

:)

Jeff Monteiro
  • 623
  • 1
  • 7
  • 13
3

use this code:

<input type="number" pattern="[0-9]*" />
Behnam Mohammadi
  • 5,246
  • 35
  • 32
  • Using pattern="[0-9]*" doesn't bring up the proper number keyboard on Iphone it has no way to switch to alphabetic or symbols keyboard and there is no decimal point either – Ikram Shah Oct 18 '18 at 07:48
2

There are other types which can display numeric keyboard.

With type="number" you can do nothing. It won't accept anything but numbers. But if you use type="tel", it's an ugly hack, but it works.

Here's my zip code example:

<input type="tel" id="Address_ZipCode" class="zip-code" pattern="^\d{2}-\d{3}$" maxlength="6">

There will however be a problem with "-" key on some screen keyboards, you can work around this problem with adding the dash after specified number of characters in JavaScript like this:

// Zip Code dashes
$('input[type="tel"].zipCode').keyup(function(event) {
    var t = event.target, v = t.value;
    if (v.length == 2) { t.value = v + '-'; }
});

(Please excuse jQuery). Instead of using type="tel" you can use type="text" and pattern property, but I haven't tested it yet. Most likely it wouldn't work with most browsers.

Harry
  • 3,090
  • 3
  • 26
  • 56
0

I couldnt find any solution for that as of now.

But one possible trick you could do is use type="number" and change it in javascript with document.getElementById("element").type="text";

but however this would also clear the ABC but it would accept numbers commas and decimals

Venkat Reddy
  • 1,035
  • 1
  • 7
  • 13
  • Yes, I have used that but the keyboard becomes default keyboard and the numeric keyboard did not open : ( – red23jordan Jun 03 '13 at 01:29
  • check it here http://jsfiddle.net/svenkatreddy/REHyd/ With This you can get the numeric keyboard and input type is still "text" but however it wont accept Alphabhets (ABC will be cleared) and Numeric keyboard will only shown first time, if you want it for multiple times just store the value of input in variable and convert back to input type "number" I'm sorry but this is the only hack available as of now. – Venkat Reddy Jun 04 '13 at 00:04
0

Try this workarround. Worked for me.

It will turn type to number then return back to text. This will force ios to switch to numeric keybord on the first prop change. The setSelectionRange is for select the input value.

$(function(){

    $("input[type='text']").on('mouseup', function(e){
        e.preventDefault();
    });

    $("input[type='text']").on('focus click', function(e){
        $(this).prop('type', 'number');
        var obj = $(this);
        setTimeout(function(){
            obj.prop('type', 'text');
            document.getElementById(obj.attr('id')).setSelectionRange(0, 9999);
        }, 50);     
    });
});