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

后端 未结 13 1873
我在风中等你
我在风中等你 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: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();
            }
    });
    

提交回复
热议问题