37

I'm trying to get regex pattern in input type number to show only numbers and dots. I tried something like this.

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

<input type="tel">

Both are showing only numbers (0-9), but not displaying . (dot). I need to use dot in input field.

Is it possible thru html5? Or Shall I go with javascript?

Note: This is working fine in Android, but . (dot) not displaying in iphones

I need to display mobile keypad like this..

enter image description here

Any help regarding this?

UI_Dev
  • 3,317
  • 16
  • 46
  • 92
  • 1
    Not sure about mobile browsers, but by default some browsers don't let you enter decimal values in a number field unless you specify `step="any"`. But either way I don't think you can force the phone browser to display any particular mobile keypad. – nnnnnn Nov 24 '14 at 11:29
  • How to force phone browser to display any particular mobile keypad? Any idea? – UI_Dev Nov 24 '14 at 11:31
  • You can't. The best you can do is try to make your own virtual keyboard, but that's a horrible idea. The mobile system will decide it's own keyboard, the only thing you can do to influence it is your type="", which obviously isn't enough for you – Gareth Parker Nov 24 '14 at 11:54
  • The problem is the keyboard software, some similar happens with Samsung mobiles & Samsung keyboard. – ojovirtual Nov 24 '14 at 11:55
  • 2
    Isn't `.` the regex wildcard? Did you try escaping it? `[0-9\.]*` – royhowie Nov 27 '14 at 06:25
  • I tried many times with this regex pattern. But in iPhone it is not displaying the . (dot) – UI_Dev Nov 27 '14 at 06:40
  • Spec: [HTML 5.2 spec section 4.10.5.3.6. The pattern attribute](https://www.w3.org/TR/html52/sec-forms.html#the-pattern-attribute) – Jared Beck Mar 27 '19 at 18:27

5 Answers5

25

If you only specify "type=number" it will display keypad on iPhone like:

enter image description here

And if you specify pattern like <input type="number" pattern="\d*"/> or <input type="number" pattern="[0-9]*" />, then keypad on iPhone will be like :

enter image description here

Still it cannot display dot(.), currently there is no pattern to handle such case.

So you may opt for <input type="tel" /> which will provide keypad like:

enter image description here

Please refer to below links for more details on inputs for iOS:

http://bradfrost.com/blog/mobile/better-numerical-inputs-for-mobile-forms/

http://blog.pamelafox.org/2012/05/triggering-numeric-keyboards-with-html5.html

https://about.zoosk.com/nb/engineering-blog/mobile-web-design-use-html5-to-trigger-the-appropriate-keyboard-for-form-inputs/

http://mobiforge.com/design-development/html5-mobile-web-forms-and-input-types

http://www.petefreitag.com/item/768.cfm

http://html5tutorial.info/html5-contact.php

Hope this will help you. :)

Updates for customization (reference: https://stackoverflow.com/a/20021657/1771795)

You can do some customization using javascript. Lets take example of currency input with decimals pattern in which e.which to read CharCode entered and then push it into an array (before) which represents digits before decimal mark and another array (after) to move values from (before) array past the decimal mark.

complete fiddle link

HTML:

<input type="tel" id="number" />

JS

Variables and functions:

// declare variables
var i = 0,
    before = [],
    after = [],
    value = [],
    number = '';

// reset all values
function resetVal() {
    i = 0;
    before = [];
    after = [];
    value = [];
    number = '';
    $("#number").val("");
    $(".amount").html("");
}

// add thousand separater
function addComma(num) {
  return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

Main code:

// listen to keyup event
$("#number").on("keyup", function (e, v) {

    // accept numbers only (0-9)
    if ((e.which >= 48) && (e.which <= 57)) {

        // convert CharCode into a number   
        number = String.fromCharCode(e.which);

        // hide value in input
        $(this).val("");

        // main array which holds all numbers
        value.push(number);

        // array of numbers before decimal mark
        before.push(value[i]);

        // move numbers past decimal mark
        if (i > 1) {
            after.push(value[i - 2]);
            before.splice(0, 1);
        }

        // final value
        var val_final = after.join("") + "." + before.join("");

        // show value separated by comma(s)
        $(this).val(addComma(val_final));

        // update counter
        i++;

        // for demo
        $(".amount").html(" " + $(this).val());

    } else {

        // reset values
        resetVal();
    }
});

Reset:

// clear arrays once clear btn is pressed
$(".ui-input-text .ui-input-clear").on("click", function () {
    resetVal();
});

Result:

enter image description here

Community
  • 1
  • 1
Mazzu
  • 2,799
  • 20
  • 30
  • These things i know. I need to display (.) in numeric keypad. How it is possible? – UI_Dev Nov 27 '14 at 11:31
  • If you were doing this in a bona fide iOS app, you could probably add buttons of your own on top of the keyboard, based on http://stackoverflow.com/questions/5824325/iphone-adding-a-button-to-keyboard-existing-accessory-view-keyboard-from-uiwe, which is about the accessory view but the same process would work. – Mazzu Nov 28 '14 at 04:11
  • Also please go through the link for JS use http://stackoverflow.com/questions/24407109/new-webkits-convert-decimal-comma-to-dot-and-vice-versa-in-number-type-inputs – Mazzu Nov 28 '14 at 04:15
  • Please read out the comment for the above linked question..Your solution DOES NOT WORK on an iPhone (tested with iOS7 or iOS8), because on the iPhone the 0-9 virtual keypad shows up, which doesn't have the normal shifted keyboard (instead it has the 0-9 keyboard with [+*#] key and no [.] or [,] key available). – UI_Dev Nov 28 '14 at 06:47
  • Such customization doesn't seem to work with iPhone :( – Mazzu Nov 28 '14 at 08:14
  • Please refer to this solution http://stackoverflow.com/questions/19684855/force-ios-numeric-keyboard-with-custom-currency-pattern hope it will work for your case :) – Mazzu Dec 02 '14 at 05:14
  • @UIDesigner, please find customization code under "Updates for customization" heading of above answer. Hope it will help you :) – Mazzu Dec 04 '14 at 05:26
  • Shouldn't you change `type="number"` for `type="text"`? I read that pattern in number is not universally accepted. – João Pimentel Ferreira Jul 24 '17 at 21:13
  • This solution is now outdated, see https://stackoverflow.com/a/59181766/786389 – Josh Hibschman Jul 09 '21 at 15:32
15

Not every input type and attribute is supported in all browsers. In general, most modern browsers from IE10+ include basics such as email and number.

The browser will revert to a standard text input when a specific type and ignore attributes when those values are not supported.

enter image description here enter image description here

So you should use a good regular expression pattern.

for example

<input type="tel" name="tel" pattern="^(?:\(\d{3}\)|\d{3})[- . ]?\d{3}[- . ]?\d{4}$" />
  • 1234567899
  • 123 456 7899
  • 123-456-7899
  • 123.456.7899

supported

Browser support for 'tel' type

  • Android (yes)
  • iOS (yes)
  • IE (yes)
  • Mobile (yes)
  • Opera (yes)
  • Mobile (yes)
  • Opera (yes)
  • Classic (yes)
  • Opera Mini (no)
  • Firefox (yes)
  • Mobile (yes)
  • Chrome for Android (yes)

(Sources: caniuse.com, DeviceAtlas, mobilehtml5.org)

Browser support for pattern attribute

But the pattern attribute is supported in Internet Explorer 10, Firefox, Opera, and Chrome. And is not supported in Internet Explorer 9 and earlier versions, or in Safari.

amirhossein693
  • 442
  • 4
  • 9
  • Even though that pattern may support decimals, unfortunately the tel keyboard on ios does not have a decimal key. – Chase Apr 29 '17 at 22:30
5

For iOS use the input attribute type="number", inputmode="decimal". This will show the number pad with the “dots” on iOS 12.3+.

Shaya Ulman
  • 1,299
  • 1
  • 12
  • 24
Bookee
  • 51
  • 1
  • 3
  • For reference, since I hit this today... using `` does indeed work in iOS Safari to present a numeric keyboard with a decimal point, i.e.: [UIKeyboardType.decimalPad](https://developer.apple.com/documentation/uikit/uikeyboardtype/decimalpad). In iOS Cordova apps, though (probably due to WKWebView differences), you get the qwerty-like numeric keyboard instead, i.e.: [UIKeyboardType.numbersAndPunctuation](https://developer.apple.com/documentation/uikit/uikeyboardtype/numbersandpunctuation). – AlwaysLearning Feb 26 '20 at 05:10
4

I had a similar scenario whereby I needed to support both comma and point as both the decimal mark and digit grouping [see here]

E.g.

1.00 / 1,00
1,000,000.00 / 1.000.000,00

At the same time the scenario required that the number keypad was displayed on mobile devices.

The initial implementation combined the 'number' type with the pattern attribute.

<input type="number" pattern="^(0*[,.]*[0-9][0-9]*([,.][0-9]+)*|[0-9]?[,.][0-9]*[1-9][0-9]*)$" required />

However the number validation failed inputs that the pattern would allow. This meant the field and thus form were marked as invalid.

The solution was to change the type to 'tel'.

<input type="tel" pattern="^(0*[,.]*[0-9][0-9]*([,.][0-9]+)*|[0-9]?[,.][0-9]*[1-9][0-9]*)$" required />

Mobile users would now see a number keypad by default, and the pattern validation would be used to validate the input.

Kildareflare
  • 4,590
  • 5
  • 51
  • 65
3

Unfortunately it's not possible to achieve the exact functionality that you're looking for is not possible. However there is a kind of "hack" available which will give similar functionality:

http://www.brownphp.com/2011/05/iphone-currency-input-web-apps/ (Link broken)

It's a bit of JS which fills in the decimal automatically when the user starts typing, like this: 0.01 -> 0.12 -> 1.23 -> 12.34 . So this can be used for similar effect.

Rick
  • 3,240
  • 2
  • 29
  • 53