How to block +,-,e in input type Number?

后端 未结 13 1777
我在风中等你
我在风中等你 2020-12-29 20:52

When I\'m giving input type number the letter e and special charecters are also displaying in input field. I want to display only digits. How to block them?

相关标签:
13条回答
  • 2020-12-29 21:06

    You can block entering those chars with keydown event

    var inputBox = document.getElementById("inputBox");
    
    var invalidChars = [
      "-",
      "+",
      "e",
    ];
    
    inputBox.addEventListener("keydown", function(e) {
      if (invalidChars.includes(e.key)) {
        e.preventDefault();
      }
    });
    <input type="number" id="inputBox" />

    but the user can still enter them if s/he does a copy/paste (or through the console). To prevent copy/paste, you can do a replace on the entered value [*].

    var inputBox = document.getElementById("inputBox");
    
    var invalidChars = [
      "-",
      "+",
      "e",
    ];
    
    inputBox.addEventListener("input", function() {
      this.value = this.value.replace(/[e\+\-]/gi, "");
    });
    
    inputBox.addEventListener("keydown", function(e) {
      if (invalidChars.includes(e.key)) {
        e.preventDefault();
      }
    });
    <input type="number" id="inputBox" />


    * You can't really get the entered value on an input field with type set to number. You can get the entered value as long as it is a number, that is, the value passes the internal number check. If the user copy/paste 1e, suggested solution will fail.

    What happens when you enter 1e is that, input field checks if it's a number, and if it's not (1e is not) it throws a warning:

    The specified value "1e" is not a valid number. The value must match to the following regular expression: -?(\d+|\d+.\d+|.\d+)([eE][-+]?\d+)?

    and the value property is set to "".

    If you check the field's properties, you'll find valueAsNumber property. If the entered value is a number, input field parses the value and stores it in valueAsNumber. Since 1e is not a number, it evaluates to NaN, and NaN is assigned to valueAsNumber and value is set to "". Though you still see 1e on the input field.

    I've asked a question related to this problem, but no solution yet.

    Get the entered value on number input field, not the parsed

    0 讨论(0)
  • 2020-12-29 21:09

    Here's a pretty concise solution using jQuery based on some of the other solutions:

    $("input[type=number]").on("keydown", function(e) {
            var invalidChars = ["-", "+", "e"]; //include "." if you only want integers
            if (invalidChars.includes(e.key)) {
                e.preventDefault();
            }
    });
    
    0 讨论(0)
  • 2020-12-29 21:10

    If you want + sign in your phone number field then you can use this code.

    var inputBox = document.getElementById("inputBox");
    
    var invalidChars = [
      "-",
      "e",
    ];
    
    inputBox.addEventListener("keydown", function(e) {
      if (invalidChars.includes(e.key)) {
        e.preventDefault();
      }
    });
    <input type="number" id="inputBox" />

    0 讨论(0)
  • 2020-12-29 21:14

    You can check it if it's a number or not.

    // Restrict e, -, + for html input number
    $(document).on('keypress', ':input[type="number"]', function (e) {
        if (isNaN(e.key)) {
            return false;
        }
    });
    
    0 讨论(0)
  • 2020-12-29 21:14

    JQuery
    You can also use the following code if you want to accept decimals(.)

    $('input[Type="Number"]').keypress(function (e) {
        if ('0123456789'.indexOf(e.key)!=-1){}
        else if (e.key=='.' && this.value!='' && (this.value.match("\.") || []).length==0){}
        else{e.preventDefault();}
    });
    
    
    0 讨论(0)
  • 2020-12-29 21:16

    Since OP's question was tagged with 'angularjs', the cleanest solution would probably be to use a directive. Several solutions for this approach have previously been explained here:

    angularjs: allows only numbers to be typed into a text box

    0 讨论(0)
提交回复
热议问题