input type=number 禁止输入字符“e”的办法

 输入框input,的type设置为number,本想只输入数字,但是字符“e”却能通过,

首先科普一下,

 <body>
        <input  onkeypress="getCode(event)"  />
 </body>
<script>
  function    getCode(e){
    console.log(e.keyCode);
  }
</script>

每次在输入框输入,我们可以拿到一个event.keyCode,他是一个unicode值。

String.fromCharCode:可以将一个unicode码转换了他对应的值

输入:b 98 ->b

现在我们已经能获取到输入的值了,接下来就是,用一个正则表达式,进行过滤

/[d]/:匹配一个数字字符

所以.......

以下禁止字符“e”的输入(应该是禁止一切字符,)

<input  type="number" onkeypress="return(/[d]/.test(String.fromCharCode(event.keyCode))"/>

 当不满足这个正则的时候,就返回一个false,输入框不显示。

原文地址:https://www.cnblogs.com/evaling/p/7655163.html