How to allow numbers, backspace, delete, left and right arrow keys in the html text?

前端 未结 5 2032
野趣味
野趣味 2021-02-05 06:25

I am using following javascript code which I think should only allow numbers, backspace, delet, left arrow and right arrow keys in textbox, but it is also allowing alphabets. I

5条回答
  •  情歌与酒
    2021-02-05 07:19

    I modified his code a bit, so you can just add a class to an input

    $(".numberonly").keydown(function(e){
        // Number Only
        if (e.keyCode == 8 || e.keyCode == 46
           || e.keyCode == 37 || e.keyCode == 39) {
              return true;
          }
          else if ( e.keyCode < 48 || e.keyCode > 57 ) {
            e.preventDefault();
          }
      });
    

    add class to an input

    
    

提交回复
热议问题