Best way to restrict a text field to numbers only?

前端 未结 29 1679
长情又很酷
长情又很酷 2020-12-04 22:01

I\'m using the following Javascript to restrict a text field on my website to only accept numerical input, and no other letters or characters. The problem is, it REALLY reje

29条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-04 22:35

      document.getElementById('myinput').onkeydown = function(e) {
          if(!((e.keyCode > 95 && e.keyCode < 106)
          || (e.keyCode > 47 && e.keyCode < 58) 
          || e.keyCode == 8
          || e.keyCode == 9)) {
            return false;
          }
      }
    

提交回复
热议问题