249

Is there a way to force the number keyboard to come up on the phone for an <input type="text">? I just realized that <input type="number"> in HTML5 is for “floating-point numbers”, so it isn’t suitable for credit card numbers, ZIP codes, etc.

I want to emulate the numeric-keyboard functionality of <input type="number">, for inputs that take numeric values other than floating-point numbers. Is there, perhaps, another appropriate input type that does that?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Tami
  • 3,221
  • 2
  • 18
  • 15
  • There still isn't a great answer for postal codes. I re-asked this question specifically for international postal codes here: http://stackoverflow.com/questions/25425181/iphone-ios-presenting-html-5-keyboard-for-postal-codes – Ryan McGeary Aug 21 '14 at 11:31
  • I figured out a hackish way to do this ..sort of: http://stackoverflow.com/a/25599024/1922144 – davidcondrey Sep 01 '14 at 05:11
  • As of mid-2015, there is a better way of doing this: http://stackoverflow.com/a/31619311/806956 – Aaron Gray Oct 06 '15 at 23:11
  • Older, closely-related question: https://stackoverflow.com/questions/3368546/what-input-field-type-forces-the-number-pad-mobile-keyboard-to-come-up-when-focu – Jon Schneider Oct 18 '21 at 21:18

16 Answers16

254

You can do <input type="text" pattern="\d*">. This will cause the numeric keyboard to appear.

See here for more detail: Text, Web, and Editing Programming Guide for iOS

<form>
  <input type="text" pattern="\d*">
  <button type="submit">Submit</button>
</form>
Cœur
  • 37,241
  • 25
  • 195
  • 267
Seth Stone
  • 2,742
  • 1
  • 14
  • 13
  • 23
    If you just want the numeric keyboard but don't want to validate (such as in Chrome), you can put the novalidate attribute on the form.
    – Brian Oct 03 '12 at 00:19
  • 4
    I have found that `\d*` does not work and `[0-9]` is required. Note that this will work with `type='numeric'`. This is suggested based on the linked documents reference for zip code. – Brett Ryan Sep 18 '13 at 11:08
  • 12
    This works as long as you need numeric values ONLY. Floating point numbers that require a '.' (or ',' for some regions) cannot be entered as they keyboard does not include these characters. – DrHall Aug 08 '14 at 18:55
  • 3
    If I need to use . (dot), how can I use it? this pattern shows only [0-9] –  Nov 24 '14 at 11:57
  • 25
    doesnt work on android? I tested on ios and worked successfully but didnt work on android. how can I solve it – kamal Apr 01 '15 at 07:43
  • If you need access to the dot, use something similar to this `` – Frederik Krautwald Jul 22 '15 at 09:54
  • Stack Overflow code snippets don't show a "run" button when viewed on mobile. You need to view as desktop site. I didn't realise this when I edited the answer! – Flimm Jan 01 '19 at 11:28
  • 1
    Doesn't work for android – Sophile Oct 22 '22 at 08:33
  • @Sophile Not sure if you still need this, but - try ``. This works in Chrome for Android (12) and iOS (16). – FiddlingAway Apr 03 '23 at 14:31
253

As of mid-2015, I believe this is the best solution:

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

This will give you the numeric keypad on both Android and iOS:

enter image description here

It also gives you the expected desktop behavior with the up/down arrow buttons and keyboard friendly up/down arrow key incrementing:

enter image description here

Try it in this code snippet:

<form>
  <input type="number" pattern="[0-9]*" inputmode="numeric">
  <button type="submit">Submit</button>
</form>

By combining both type="number" and pattern="[0-9]*, we get a solution that works everywhere. And, its forward compatible with the future HTML 5.1 proposed inputmode attribute.

Note: Using a pattern will trigger the browser's native form validation. You can disable this using the novalidate attribute, or you can customize the error message for a failed validation using the title attribute.


If you need to be able to enter leading zeros, commas, or letters - for example, international postal codes - check out this slight variant.


Credits and further reading:

http://www.smashingmagazine.com/2015/05/form-inputs-browser-support-issue/ http://danielfriesen.name/blog/2013/09/19/input-type-number-and-ios-numeric-keypad/

Flimm
  • 136,138
  • 45
  • 251
  • 267
Aaron Gray
  • 11,283
  • 7
  • 55
  • 61
  • 2
    The HTML5 `type="number"` [spec](https://html.spec.whatwg.org/multipage/forms.html#number-state-(type=number)) says *The following content attributes must not be specified and do not apply to the element: ... inputmode* – Tgr Nov 23 '16 at 19:56
  • 2
    @Tgr Ahh, good catch. It appears the spec wants people to use `` and it wants mobile browsers to look at `inputmode` to determine if it shows a numpad or full keyboard. Since no browsers support `inputmode`, I'm unsure how its going to be implemented on desktop vs. mobile browsers. Ex: Will desktop browsers allow letter typing in ``? If so, that's a problem. `` prevents this, only allowing numbers. Perhaps we'll have to wait until browsers start implementing it and then we can update this answer. – Aaron Gray Nov 23 '16 at 21:34
  • 2
    Support for `type="number"` is [decent](http://caniuse.com/#feat=input-number), but it might display a spinner which does not make much sense for e.g. credit card numbers. – Tgr Nov 23 '16 at 23:15
  • 1
    how can I restrict to only two digits? maxlength does not work on number fields... – Björn Dec 01 '16 at 16:45
  • 2
    Having type="number" is a pain in the ass X(. If the input is invalid, you won't be able to get the value of the input in JS, you can't get the cursor position, etc. – Nicu Surdu Feb 27 '17 at 11:24
  • When center aligning the text in the text field, due to the up/down arrow buttons that are there, the text is slightly on the left instead of being perfectly centered. Is there a way to fix this? – Howard Jun 01 '17 at 23:52
  • 1
    @Hans add min="0" max="99" – Howard Jun 01 '17 at 23:55
  • 2
    `This attribute applies when the value of the type attribute is text, search, tel, url, email, or password, otherwise it is ignored.` – Michael Laffargue Jul 27 '17 at 13:24
  • 1
    this is a greate solution, but can i put the type="text" and the inputmode="numeric" on the same input field for credit card number as the following: `` or that will be not semantic or even may cause a problem? – Dani Mousa Feb 13 '20 at 20:23
  • declaring `` worked just as well for me, allowing myself to do all the custom validation. This *particular* case was for a monetary value. Showing the number pad on a mobile whilst allowing for commas to be added in on the thousandth's – Guybrush Threepwood Apr 27 '21 at 08:18
  • According to MDN, pattern is not a valid attribute for type="number" inputs https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input#pattern – Michał Terczyński Jul 18 '22 at 10:48
  • For those of you looking for more ways to show a number pad on the phone, try ```inputmode="decimal"``` – Jorge Mauricio Aug 12 '22 at 20:33
69

I have found that, at least for "passcode"-like fields, doing something like <input type="tel" /> ends up producing the most authentic number-oriented field and it also has the benefit of no autoformatting. For example, in a mobile application I developed for Hilton recently, I ended up going with this:

iPhone Web Application Display with an Input Tag Having a Type of TEL which Produces a very Decent Numeric Keyboard as Opposed to Type Number which is Autoformatted and Has a Somewhat Less Intuitive Input Configuration

... and my client was very impressed.

<form>
  <input type="tel" />
  <button type="submit">Submit</button>
</form>
Flimm
  • 136,138
  • 45
  • 251
  • 267
Gabriel Ryan Nahmias
  • 2,135
  • 2
  • 26
  • 39
  • 4
    The problem for me was not auto-formatting, but validation. Input of type `number` forces strict rules for the number in many browsers. If user adds space (or comma) to separate thousands you will not get ANY value from the input. Yes, even in JavaScript `input.value` will be empty. Input of type `tel` is not restricted to specific format so user can still input anything and validation is a separate process (for developer to handle). – Nux Mar 10 '15 at 08:37
  • 1
    How can i use decimal numbers in this keyboard – Anto Jul 09 '15 at 12:16
27
<input type="text" inputmode="numeric">

With Inputmode you can give a hint to the browser.

Ralf de Kleine
  • 11,464
  • 5
  • 45
  • 87
15

Using the type="email" or type="url" will give you a keyboard on some phones at least, such as iPhone. For phone numbers, you can use type="tel".

Niklas
  • 29,752
  • 5
  • 50
  • 71
12

as of 2020

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

css tricks did a really good article on it: https://css-tricks.com/finger-friendly-numerical-inputs-with-inputmode/

James Dinnes
  • 131
  • 2
  • 3
11

There is a danger with using the <input type="text" pattern="\d*"> to bring up the numeric keyboard. On firefox and chrome, the regular expression contained within the pattern causes the browser to validate the input to that expression. errors will occur if it doesn't match the pattern or is left blank. Be aware of unintended actions in other browsers.

Mike
  • 111
  • 1
  • 3
  • 6
    Adding a `novalidate` attribute on the containing `form element` should deal with the auto-validation problems. Be sure to do some validation on your own. – Justus Romijn Aug 08 '14 at 08:39
4

For me the best solution was:

For integer numbers, which brings up the 0-9 pad on android and iphone

<label for="ting">
<input id="ting" name="ting" type="number" pattern="[\d]*" />

You also may want to do this to hide the spinners in firefox/chrome/safari, most clients think they look ugly

 input[type=number]::-webkit-inner-spin-button,
 input[type=number]::-webkit-outer-spin-button {
      -webkit-appearance: none;
      margin: 0;
 }

 input[type=number] {
      -moz-appearance:textfield;
 }

And add novalidate='novalidate' to your form element, if your doing custom validation

Ps just in case you actually wanted floating point numbers after all,step to whatever precision you fancy, will add '.' to android

<label for="ting">
<input id="ting" name="ting" type="number" pattern="[\d\.]*" step="0.01" />
aqm
  • 2,942
  • 23
  • 30
3

I think type="number" is the best for semantic web page. If you just want to change the keyboard, you can use type="number" or type="tel". In both cases, iPhone doesn't restrict user input. User can still type in (or paste in) any characters he/she wants. The only change is the keyboard shown to the user. If you want any restriction beyond this, you need to use JavaScript.

Cat Chen
  • 2,387
  • 17
  • 12
  • 9
    The issue I have is that type="number" is only supposed to be for floating point numbers - so no credit cards, zip codes, or a slew of other numbers only strings. I was hoping there was both a semantic way of encoding it as well as making the right keyboard display. The appropriate type, I think, would be input type="text" but then the wrong keyboard displays. If you could point me to some javascript to do it, that would solve both problems. – Tami Jun 03 '11 at 05:01
  • 2
    `type="number"` is for float. `type="text"` is for string. What you really want is integer. I think float is closer to integer than string. What do you think? – Cat Chen Jun 04 '11 at 13:40
  • 6
    One thing to beware of with type="number" is that Chrome at least will force the input to be a number - which will break credit card numbers entered with spaces (for example) – John Carter Jun 27 '12 at 03:21
  • 8
    @therefromhere It will also turn zip codes like `01920` to `1920`. – ceejayoz Aug 08 '13 at 13:44
  • it will also mess up if the user has a different language set in their browser http://stackoverflow.com/questions/5345095/chrome-auto-formats-input-number – dalore Mar 13 '14 at 14:31
3

There is an easy way to achieve this type of behaviour(like if we want to use text formatting in the input field but still want the numeric keyboard to be shown):

See this screenshot

My Input:

<input inputMode="numeric" onChange={handleInputChange} />

Tip: if you want this type of behaviour(comma-separated numbers) then follow handleInputChange implementation (this is react based so mentioned states also)

see the React code here

kiner_shah
  • 3,939
  • 7
  • 23
  • 37
1

In 2018:

<input type="number" pattern="\d*">

is working for both Android and iOS.

I tested on Android (^4.2) and iOS (11.3)

1
 <input type="text" inputmode="decimal">

it will give u text input using numeric key-pad

1

All of the posted answers trigger the number only keyboard, which is not what the OP was asking. The only way I've found to trigger the type='number' keyboard on a text input is to use CSS and JS.

The trick is to create a second number input, which you overlap on top of your text input using css.

<style type="text/css">
    /* hide number spinners on inputs */
    input::-webkit-outer-spin-button,
    input::-webkit-inner-spin-button {
        -webkit-appearance: none;
        margin: 0;
    }
    input[type=number] {
        -moz-appearance:textfield; /* Firefox */
    }
    .hidden-number {
        margin-top: -26px;
    }
</style>


<form method="post" action="submit.php">
    <input class="form-control" type="text" name="input_name">
    <input class="form-control hidden-number" type="number" id="input_name">
</form>

Using JavaScript, when your number input gains focus it will trigger the keyboard that you want. You will then have to remove the type='number' attribute, which would prevent you from entering anything other than numbers. Then transfer whatever content is in your text input to the number input. Lastly, when the number input loses focus, transfer its contents back to the text input and replace its type='number' attribute.

<script type="text/javascript">
    $(".hidden-number").on("focus", function(){
        $(this).removeAttr("type");
        var text_input = $("input[name="+this.id+"]");
        $(this).val(text_input.val());
        text_input.val("");
    });

    $(".hidden-number").on("focusout", function(){
        var text_input = $("input[name="+this.id+"]");
        text_input.val($(this).val());
        $(this).attr("type", "number");
    });
</script>
brassmookie
  • 349
  • 2
  • 13
0

You can try like this:

<input type="number" name="input">
<input type="submit" value="Next" formnovalidate="formnovalidate">

But be careful: If your input contains something other than a number, it will not be transmitted to the server.

alexwenzel
  • 2,361
  • 2
  • 15
  • 18
0

I couldn't find a type that worked best for me in all situations: I needed to default to numeric entry (entry of "7.5" for example) but also at certain times allow text ("pass" for example). Users wanted a numeric keypad (entry of 7.5 for example) but occasional text entry was required ("pass" for example).

Rather what I did was to add a checkbox to the form and allow the user to toggle my input (id="inputSresult") between type="number" and type="text".

<input type="number" id="result"... >
<label><input id="cbAllowTextResults" type="checkbox" ...>Allow entry of text results.</label>

Then I wired a click handler to the checkbox that toggles the type between text and number based on whether the checkbox above is checked:

$(document).ready(function () {
    var cb = document.getElementById('cbAllowTextResults');
    cb.onclick = function (event) {
        if ($("#cbAllowTextResults").is(":checked"))
            $("#result").attr("type", "text");
        else
            $("#result").attr("type", "number");

    }
});

This worked out well for us.

Jeff Mergler
  • 1,384
  • 20
  • 27
-1

try this:

$(document).ready(function() {
    $(document).find('input[type=number]').attr('type', 'tel');
});

refer: https://answers.laserfiche.com/questions/88002/Use-number-field-input-type-with-Field-Mask

Dmitriy
  • 5,525
  • 12
  • 25
  • 38