35

I tried the <input type="number" /> but on Opera that outputs a strange input box coupled with an "up and down" handler. What I expected was a regular text field that once you focus on it prompts the number keyboard instead of the alphabets. Is that even possible?

p.s. I'm not trying to validate. It would be a nice user experience, that's all.

Michael Geary
  • 28,450
  • 9
  • 65
  • 75
Ayyash
  • 4,257
  • 9
  • 39
  • 58

7 Answers7

50

Use pattern="[0-9]*"

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

  • Example phone input: <input type="tel" pattern="[0-9]*" />

Note: Browsers that do not support type="tel" will default to a text type

Beware: Using type="number" can cause problems with some browsers and user experience for credit card, postal code, and telephone inputs where a user might need to enter punctuation or a comma being in the output.

References:

Huski
  • 958
  • 1
  • 7
  • 14
23

The official HTML5 way to handle phone numbers is:

<input type="tel">

You may not have liked the "strange input box" you got with Opera when you used<input type="number" />, but that really is the appropriate type of input area when you want to require visitors to enter a numeric value.

Dori
  • 915
  • 1
  • 12
  • 20
  • but the "type=number" did not prompt the number pad, i'll try the tel type – Ayyash Jul 31 '10 at 02:12
  • im using opera 10 on windows mobile 6.5, i think its got some HTML5 in it, i know ive seen work, input type didnt work though – Ayyash Jul 31 '10 at 02:54
  • The `type="number"` attribute isn't necessarily supposed to bring up a number pad. The question is: are you trying to force users to enter in a numeric value, or enter in a phone number? For the former, `type="number"` is what you want. For the latter, `type="tel"` (the telephone keypad, including # and *) is what you're looking for. `` isn't an input field; it's a link to dial a phone number. Example: `1-408-555-5555`. – Dori Jul 31 '10 at 05:01
  • 2
    @Ayyash, I'm not pissed off—I just think you're not paying attention. You asked about input in Mobile Opera. I told you how input works for the browsers installed on many 2010-era mobile phones. @fravelgue told you that many phones don't support HTML (irrelevant, because Opera does), how one type of link works (irrelevant, because they're not inputs), and about a 2001 WAP CSS specification which doesn't apply to Opera or to WAP v1 or to HTML input fields. And btw: no, you never did say that `input type=tel` didn't work for you—you said "i'll try the tel type" but never reported your results. – Dori Aug 05 '10 at 01:48
12

type="number" is HTML5 and many phones do not support HTML5. For call link you can use type="tel" or <A href="wtai://wp/mc;600112233">Special A</A>. You should look at CSS WAP extensions (page 56) too.

EDIT 10/2015:
Most if not ALL smart phones support HTML5 and CSS3, so type="number" is the best way.

ajon
  • 7,868
  • 11
  • 48
  • 86
fravelgue
  • 2,633
  • 2
  • 24
  • 23
10

This post is now invalid. All smartphones support HTML5 and CSS3 now, so adding type="number" does in fact prompt the number pad to pop-up. I just checked it on 2 different Android versions, and an iPhone. Just so no one in the future tries WAP instead of the correct HTML5 format.

Jeff Walters
  • 161
  • 3
  • 11
  • 10
    Based on my testing, `type='number'` pulls up the full keyboard with numbers visible on iOS, whereas `type='tel'` pulls up the number pad, which seems to be what the poster is asking about. On Android, both `number` and `tel` pull up the number pad. – Stan Jan 29 '16 at 18:54
2

This will work on mobile and will prevent the letter "e" (along with all other letters) from being allowed to be typed in in the desktop version of your page. type="number" by itself still normally allows "e" per spec:

<input pattern="[0-9]*" type="text" oninput="this.value=this.value.replace(/[^0-9]/g,'');">

If you use type="number" in the above, then if you type "123" then "e" the oninput JS will replace all contents of the box. Just use type="text" if you really just want integer values.

d35348w
  • 151
  • 1
  • 9
2

You can control the style of keyboard that comes up on input focus, independently of the input type, with the HTML attribute inputmode. What you're probably looking for is inputmode="numeric", which shows a number pad with 0-9. There are other options, such as a number pad with # and *. See the docs linked below.

This is ideal for uses cases where type="number" would not work, such as numbers formatted with dashes.

https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/inputmode

Marc M.
  • 3,631
  • 4
  • 32
  • 53
0

Try <input type="number" pattern="/d*"> OR <input type="tel" pattern="/d*"> This will help if you working with Android.

souvik
  • 3
  • 3