21

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.

9
  • What's wrong with using type="number"? Commented May 9, 2013 at 7:06
  • Hi deceze, I have editted my question. Thanks. Commented May 9, 2013 at 7:17
  • 6
    There is, in HTML5.1, an attribute called "inputmode" which is the intended way of supporting this. So you'd have <input type="text" inputmode="numeric" name="whatever">However, it's not listed in HTML5, so I guess it's not currently implemented much if at all. Commented May 9, 2013 at 9:48
  • Solution here: stackoverflow.com/a/25599024/1922144 Commented Sep 1, 2014 at 5:12
  • 3
    @Alohci inputmode="numeric" no longer works. Commented Oct 3, 2016 at 11:12

7 Answers 7

25

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)

Sign up to request clarification or add additional context in comments.

1 Comment

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
16

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.

:)

4 Comments

there is no dot in iphone
Oh yeah! May you will needs to use masks ;)
What does masks mean?
6

use this code:

<input type="number" pattern="[0-9]*" />

1 Comment

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
3

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.

1 Comment

using type="tel" on iphone brings up a keyboard that has no way of entering a decimal point
2

I tested a few options on different iOS devices

The most supported way is to use the pattern attribute, e.g <input type="text" pattern="[0-9]*" />, since it works on several iOS versions:

Iphone 13 (iOS 15.1)

enter image description here

Iphone 8 (iOS 11.4)

enter image description here

Iphone 6 (iOS 8.1)

enter image description here

If you only need to support iOS 12.2+, using only the inputmode attribute works fine

enter image description here

1 Comment

do you how to show negative key in the keyboard?
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

2 Comments

Yes, I have used that but the keyboard becomes default keyboard and the numeric keyboard did not open : (
check it here 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.
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);     
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.