移动设备中带有正则表达式模式的html5输入类型编号

时间:2014-11-24 11:21:55

标签: html ios css html5 numpad

我正在尝试在输入类型number中获取正则表达式模式以仅显示数字和点。 我试过这样的事情。

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

<input type="tel">

两者都只显示数字(0-9),但不显示。 (点)。我需要在输入字段中使用点。

是否可以通过html5?或者我应该使用javascript?

注意:这在Android中运行良好,但是。 (点)不在iphone中显示

我需要像这样显示移动键盘..

enter image description here

对此有何帮助?

4 个答案:

答案 0 :(得分:20)

如果您只指定&#34; type = number&#34;它将在iPhone上显示键盘,如:

enter image description here

如果您指定的模式如<input type="number" pattern="\d*"/><input type="number" pattern="[0-9]*" />,那么iPhone上的键盘就像:

enter image description here

仍然无法显示点(。),目前没有处理此类情况的模式。

因此,您可以选择提供键盘的<input type="tel" />

enter image description here

有关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

希望这会对你有所帮助。 :)

自定义更新(参考:https://stackoverflow.com/a/20021657/1771795

您可以使用javascript进行一些自定义。 让我们以带有小数模式的货币输入为例,输入e.which读取CharCode,然后将其推入一个数组(之前),表示十进制标记前的数字,另一个数组(之后)移动值(之前)数组超过小数点。

complete fiddle link

<强> HTML:

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

<强> JS

变量和函数:

// 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, ",");
}

主要代码:

// 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();
    }
});

重置

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

<强>结果:

enter image description here

答案 1 :(得分:14)

并非所有浏览器都支持所有输入类型和属性。一般来说,IE10 +的大多数现代浏览器都包含电子邮件和数字等基础知识。

当特定类型时,浏览器将恢复为标准文本输入,并在不支持这些值时忽略属性。

enter image description here enter image description here

所以你应该使用一个好的正则表达式模式。

例如

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

支持的

浏览器支持&#39; tel&#39;输入

  • Android (是)
  • iOS (是)
  • IE (是)
  • 移动(是)
  • Opera (是)
  • 移动(是)
  • Opera (是)
  • 经典(是)
  • Opera Mini (不)
  • Firefox (是)
  • 移动(是)
  • 适用于Android的Chrome (是)

(来源:caniuse.com,DeviceAtlas,mobilehtml5.org)

浏览器支持模式属性

但Internet Explorer 10,Firefox,Opera和Chrome支持pattern属性。 Internet Explorer 9及更早版本 Safari 不支持。

答案 2 :(得分:2)

不幸的是,无法实现您正在寻找的确切功能。然而,有一种“hack”可用,它将提供类似的功能:

http://www.brownphp.com/2011/05/iphone-currency-input-web-apps/

当用户开始输入时,它会自动填充小数点,如下所示:0.01 - &gt; 0.12 - &gt; 1.23 - &gt; 12.34。所以这可以用于类似的效果。

答案 3 :(得分:0)

对于iOS,请使用输入属性type="number"inputmode="decimal"。 这将在iOS 12.3+上显示带有“点”的数字键盘。