简体   繁体   English

HTML5输入类型编号不能阻止加号,减号

[英]Html5 input type number not prevent plus, minus symbols

I am using input type number and It's preventing character but it's unable to prevent plus, minus symbol or character. 我正在使用输入类型数字,并且它是防止字符,但是无法防止加号,减号或字符。 But I require to prevent all symbol. 但是我要求防止所有符号。

<input type="number">

I have worked on it and create my customise regex and it's working fine but I want to know why it's working like that for input type number and how we can fix that. 我已经处理了它并创建了我的自定义正则表达式,并且可以正常工作,但是我想知道为什么输入类型编号可以正常工作,以及如何解决该问题。

You can do it with simple and a minimum limit for your number. 您可以使用简单的最低数量限制来做到这一点。 Something like this: 像这样:

<input min="0" type="number" step="1">

Now adding + or - signs make field value invalid and step=1 is for when you only need unsigned integers. 现在添加+-号使字段值无效,而step=1则用于只需要无符号整数的情况。 If you don't need that, simply remove it. 如果不需要,只需将其删除。

Edit 编辑

If you don't want to + and - signs show in the filed, plus don't accept that input, you need to use some js. 如果您不想在字段中显示+-号,并且不接受该输入,则需要使用一些js。 Here is the simplest pure js solution I think: 这是我认为最简单的纯js解决方案:

Your html code: 您的html代码:

<input min="0: type="number" step="1" onkeypress="removeSigns()" id="my-input">

And now you need to add this little script to your page: 现在,您需要将此小脚本添加到页面中:

function removeSigns() {
    var input = document.getElementById('my-input');
    input.value = parseInt(input.value.toString().replace('+', '').replace('-', ''))

This script gets the element that has my-element id, then overwrite its value. 该脚本获取具有my-element id my-element ,然后覆盖其值。

The new value is int version of old value after replacing + and - signs in it with an empty string(removing them from the string in reality). 新值是旧值的int版本,将+-替换为空字符串(实际上将其从字符串中删除)。 This solution is good when you only have one input, if you had more number input, you should change the removeStrin to a version that gives input object from this in the onkeypress . 该解决方案是好,当你只有一个输入,如果你有更多数量的输入,你应该改变removeStrin一个版本,让输入对象,从thisonkeypress But I don't add that version because of simplicity of the solution. 但是由于解决方案的简单性,我不添加该版本。 } }

Because that's exactly how the spec says it should work. 因为这正是规范所说的应该工作的方式。 The number input can accept floating point numbers , including negative symbols and the e or E character: 输入的数字可以接受浮点数 ,包括负eE字符:

A floating-point number consists of the following parts, in exactly the following order: 浮点数由以下几部分组成,按照以下顺序:

  1. Optionally, the first character may be a " - " character. 可选地,第一个字符可以是“ - ”字符。
  2. One or more characters in the range " 0—9 ". 一个或多个字符,范围为​​“ 0—9 ”。
  3. Optionally, the following parts, in exactly the following order: (可选)以下部分,按完全相同的顺序排列:
    1. a " . " character 一个.字符
    2. one or more characters in the range " 0—9 " 一个或多个字符,范围为​​“ 0—9
  4. Optionally, the following parts, in exactly the following order: (可选)以下部分,按完全相同的顺序排列:
    1. a " e " character or " E " character 一个“ e ”字符或“ E ”字符
    2. optionally, a " - " character or " + " character (可选)“ - ”字符或“ + ”字符
    3. One or more characters in the range " 0—9 ". 一个或多个字符,范围为​​“ 0—9 ”。

You can do this using jquery 您可以使用jquery做到这一点

$('input').keypress(function (e) {
    var txt = String.fromCharCode(e.which);
    if (!txt.match(/[0-9]/)) {
        return false;
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM